/[gentoo-projects]/pax-utils/paxelf.c
Gentoo

Contents of /pax-utils/paxelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.78 - (show annotations) (download) (as text)
Sat Nov 24 20:16:26 2012 UTC (21 months ago) by vapier
Branch: MAIN
Changes since 1.77: +29 -21 lines
File MIME type: text/x-csrc
sync elf.h settings

1 /*
2 * Copyright 2003-2012 Gentoo Foundation
3 * Distributed under the terms of the GNU General Public License v2
4 * $Header: /var/cvsroot/gentoo-projects/pax-utils/paxelf.c,v 1.77 2012/11/24 19:53:49 vapier Exp $
5 *
6 * Copyright 2005-2012 Ned Ludd - <solar@gentoo.org>
7 * Copyright 2005-2012 Mike Frysinger - <vapier@gentoo.org>
8 */
9
10 #include "paxinc.h"
11
12 /*
13 * Setup a bunch of helper functions to translate
14 * binary defines into readable strings.
15 */
16 #define QUERY(n) { #n, n }
17 typedef const struct {
18 const char *str;
19 int value;
20 } pairtype;
21 static inline const char *find_pairtype(pairtype *pt, int type)
22 {
23 int i;
24 for (i = 0; pt[i].str; ++i)
25 if (type == pt[i].value)
26 return pt[i].str;
27 return "UNKNOWN_TYPE";
28 }
29
30 /* translate misc elf EI_ defines */
31 static pairtype elf_ei_class[] = {
32 QUERY(ELFCLASSNONE),
33 QUERY(ELFCLASS32),
34 QUERY(ELFCLASS64),
35 { 0, 0 }
36 };
37 static pairtype elf_ei_data[] = {
38 QUERY(ELFDATANONE),
39 QUERY(ELFDATA2LSB),
40 QUERY(ELFDATA2MSB),
41 { 0, 0 }
42 };
43 static pairtype elf_ei_version[] = {
44 QUERY(EV_NONE),
45 QUERY(EV_CURRENT),
46 { 0, 0 }
47 };
48 static pairtype elf_ei_osabi[] = {
49 QUERY(ELFOSABI_NONE),
50 QUERY(ELFOSABI_SYSV),
51 QUERY(ELFOSABI_HPUX),
52 QUERY(ELFOSABI_NETBSD),
53 QUERY(ELFOSABI_GNU),
54 QUERY(ELFOSABI_LINUX),
55 QUERY(ELFOSABI_SOLARIS),
56 QUERY(ELFOSABI_AIX),
57 QUERY(ELFOSABI_IRIX),
58 QUERY(ELFOSABI_FREEBSD),
59 QUERY(ELFOSABI_TRU64),
60 QUERY(ELFOSABI_MODESTO),
61 QUERY(ELFOSABI_OPENBSD),
62 QUERY(ELFOSABI_ARM_AEABI),
63 QUERY(ELFOSABI_ARM),
64 QUERY(ELFOSABI_STANDALONE),
65 { 0, 0 }
66 };
67 const char *get_elfeitype(int ei_type, int type)
68 {
69 switch (ei_type) {
70 case EI_CLASS: return find_pairtype(elf_ei_class, type);
71 case EI_DATA: return find_pairtype(elf_ei_data, type);
72 case EI_VERSION: return find_pairtype(elf_ei_version, type);
73 case EI_OSABI: return find_pairtype(elf_ei_osabi, type);
74 }
75 return "UNKNOWN_EI_TYPE";
76 }
77
78 /* translate elf ET_ defines */
79 static pairtype elf_etypes[] = {
80 QUERY(ET_NONE),
81 QUERY(ET_REL),
82 QUERY(ET_EXEC),
83 QUERY(ET_DYN),
84 QUERY(ET_CORE),
85 { 0, 0 }
86 };
87
88 int get_etype(elfobj *elf)
89 {
90 int type;
91 if (elf->elf_class == ELFCLASS32)
92 type = EGET(EHDR32(elf->ehdr)->e_type);
93 else
94 type = EGET(EHDR64(elf->ehdr)->e_type);
95 return type;
96 }
97
98 const char *get_elfetype(elfobj *elf)
99 {
100 return find_pairtype(elf_etypes, get_etype(elf));
101 }
102
103 const char *get_endian(elfobj *elf)
104 {
105 switch (elf->data[EI_DATA]) {
106 case ELFDATA2LSB: return "LE";
107 case ELFDATA2MSB: return "BE";
108 default: return "??";
109 }
110 }
111
112 static int arm_eabi_poker(elfobj *elf)
113 {
114 unsigned int emachine, eflags;
115
116 if (ELFOSABI_NONE != elf->data[EI_OSABI])
117 return -1;
118
119 if (elf->elf_class == ELFCLASS32) {
120 emachine = EHDR32(elf->ehdr)->e_machine;
121 eflags = EHDR32(elf->ehdr)->e_flags;
122 } else {
123 emachine = EHDR64(elf->ehdr)->e_machine;
124 eflags = EHDR64(elf->ehdr)->e_flags;
125 }
126
127 if (EGET(emachine) == EM_ARM)
128 return EF_ARM_EABI_VERSION(EGET(eflags)) >> 24;
129 else
130 return -1;
131 }
132
133 const char *get_elf_eabi(elfobj *elf)
134 {
135 static char buf[26];
136 int eabi = arm_eabi_poker(elf);
137 if (eabi >= 0)
138 snprintf(buf, sizeof(buf), "%i", eabi);
139 else
140 strcpy(buf, "?");
141 return buf;
142 }
143
144 const char *get_elfosabi(elfobj *elf)
145 {
146 const char *str = get_elfeitype(EI_OSABI, elf->data[EI_OSABI]);
147 if (strncmp(str, "ELFOSABI_", 9) == 0)
148 str += 9;
149 return str;
150 }
151
152 void print_etypes(FILE *stream)
153 {
154 int i, wrap = 0;
155 for (i = 0; elf_etypes[i].str; ++i) {
156 fprintf(stream, " (%4x) = %-10s", elf_etypes[i].value, elf_etypes[i].str);
157 if (++wrap >= 4) {
158 fprintf(stream, "\n");
159 wrap = 0;
160 }
161 }
162 if (wrap)
163 fprintf(stream, "\n");
164 }
165
166 int etype_lookup(const char *str)
167 {
168 if (*str == 'E') {
169 int i;
170 for (i = 0; elf_etypes[i].str; ++i) {
171 if (strcmp(str, elf_etypes[i].str) == 0)
172 return elf_etypes[i].value;
173 }
174 }
175 return atoi(str);
176 }
177
178 /* translate elf EM_ defines */
179 static pairtype elf_emtypes[] = {
180 QUERY(EM_NONE),
181 QUERY(EM_M32),
182 QUERY(EM_SPARC),
183 QUERY(EM_386),
184 QUERY(EM_68K),
185 QUERY(EM_88K),
186 QUERY(EM_860),
187 QUERY(EM_MIPS),
188 QUERY(EM_S370),
189 QUERY(EM_MIPS_RS3_LE),
190 QUERY(EM_PARISC),
191 QUERY(EM_VPP500),
192 QUERY(EM_SPARC32PLUS),
193 QUERY(EM_960),
194 QUERY(EM_PPC),
195 QUERY(EM_PPC64),
196 QUERY(EM_S390),
197 QUERY(EM_V800),
198 QUERY(EM_FR20),
199 QUERY(EM_RH32),
200 QUERY(EM_RCE),
201 QUERY(EM_ARM),
202 QUERY(EM_FAKE_ALPHA),
203 QUERY(EM_SH),
204 QUERY(EM_SPARCV9),
205 QUERY(EM_TRICORE),
206 QUERY(EM_ARC),
207 QUERY(EM_H8_300),
208 QUERY(EM_H8_300H),
209 QUERY(EM_H8S),
210 QUERY(EM_H8_500),
211 QUERY(EM_IA_64),
212 QUERY(EM_MIPS_X),
213 QUERY(EM_COLDFIRE),
214 QUERY(EM_68HC12),
215 QUERY(EM_MMA),
216 QUERY(EM_PCP),
217 QUERY(EM_NCPU),
218 QUERY(EM_NDR1),
219 QUERY(EM_STARCORE),
220 QUERY(EM_ME16),
221 QUERY(EM_ST100),
222 QUERY(EM_TINYJ),
223 QUERY(EM_X86_64),
224 QUERY(EM_PDSP),
225 QUERY(EM_FX66),
226 QUERY(EM_ST9PLUS),
227 QUERY(EM_ST7),
228 QUERY(EM_68HC16),
229 QUERY(EM_68HC11),
230 QUERY(EM_68HC08),
231 QUERY(EM_68HC05),
232 QUERY(EM_SVX),
233 QUERY(EM_ST19),
234 QUERY(EM_VAX),
235 QUERY(EM_CRIS),
236 QUERY(EM_JAVELIN),
237 QUERY(EM_FIREPATH),
238 QUERY(EM_ZSP),
239 QUERY(EM_MMIX),
240 QUERY(EM_HUANY),
241 QUERY(EM_PRISM),
242 QUERY(EM_AVR),
243 QUERY(EM_FR30),
244 QUERY(EM_D10V),
245 QUERY(EM_D30V),
246 QUERY(EM_V850),
247 QUERY(EM_M32R),
248 QUERY(EM_MN10300),
249 QUERY(EM_MN10200),
250 QUERY(EM_PJ),
251 QUERY(EM_OPENRISC),
252 QUERY(EM_ARC_A5),
253 QUERY(EM_XTENSA),
254 QUERY(EM_VIDEOCORE),
255 QUERY(EM_TMM_GPP),
256 QUERY(EM_NS32K),
257 QUERY(EM_TPC),
258 QUERY(EM_SNP1K),
259 QUERY(EM_ST200),
260 QUERY(EM_IP2K),
261 QUERY(EM_MAX),
262 QUERY(EM_CR),
263 QUERY(EM_F2MC16),
264 QUERY(EM_MSP430),
265 QUERY(EM_BLACKFIN),
266 QUERY(EM_SE_C33),
267 QUERY(EM_SEP),
268 QUERY(EM_ARCA),
269 QUERY(EM_UNICORE),
270 QUERY(EM_ALPHA),
271 QUERY(EM_TILEPRO),
272 QUERY(EM_TILEGX),
273 { 0, 0 }
274 };
275
276 int get_emtype(elfobj *elf)
277 {
278 int type;
279 if (elf->elf_class == ELFCLASS32)
280 type = EGET(EHDR32(elf->ehdr)->e_machine);
281 else
282 type = EGET(EHDR64(elf->ehdr)->e_machine);
283 return type;
284 }
285
286 const char *get_elfemtype(elfobj *elf)
287 {
288 return find_pairtype(elf_emtypes, get_emtype(elf));
289 }
290
291 /* translate elf PT_ defines */
292 static pairtype elf_ptypes[] = {
293 QUERY(PT_NULL),
294 QUERY(PT_LOAD),
295 QUERY(PT_DYNAMIC),
296 QUERY(PT_INTERP),
297 QUERY(PT_NOTE),
298 QUERY(PT_SHLIB),
299 QUERY(PT_PHDR),
300 QUERY(PT_TLS),
301 QUERY(PT_GNU_EH_FRAME),
302 QUERY(PT_GNU_STACK),
303 QUERY(PT_GNU_RELRO),
304 QUERY(PT_PAX_FLAGS),
305 { 0, 0 }
306 };
307 const char *get_elfptype(int type)
308 {
309 return find_pairtype(elf_ptypes, type);
310 }
311
312 /* translate elf PT_ defines */
313 static pairtype elf_dtypes[] = {
314 QUERY(DT_NULL),
315 QUERY(DT_NEEDED),
316 QUERY(DT_PLTRELSZ),
317 QUERY(DT_PLTGOT),
318 QUERY(DT_HASH),
319 QUERY(DT_STRTAB),
320 QUERY(DT_SYMTAB),
321 QUERY(DT_RELA),
322 QUERY(DT_RELASZ),
323 QUERY(DT_RELAENT),
324 QUERY(DT_STRSZ),
325 QUERY(DT_SYMENT),
326 QUERY(DT_INIT),
327 QUERY(DT_FINI),
328 QUERY(DT_SONAME),
329 QUERY(DT_RPATH),
330 QUERY(DT_SYMBOLIC),
331 QUERY(DT_REL),
332 QUERY(DT_RELSZ),
333 QUERY(DT_RELENT),
334 QUERY(DT_PLTREL),
335 QUERY(DT_DEBUG),
336 QUERY(DT_TEXTREL),
337 QUERY(DT_JMPREL),
338 QUERY(DT_BIND_NOW),
339 QUERY(DT_INIT_ARRAY),
340 QUERY(DT_FINI_ARRAY),
341 QUERY(DT_INIT_ARRAYSZ),
342 QUERY(DT_FINI_ARRAYSZ),
343 QUERY(DT_RUNPATH),
344 QUERY(DT_FLAGS),
345 QUERY(DT_ENCODING),
346 QUERY(DT_PREINIT_ARRAY),
347 QUERY(DT_PREINIT_ARRAYSZ),
348 QUERY(DT_GNU_PRELINKED),
349 QUERY(DT_GNU_CONFLICTSZ),
350 QUERY(DT_GNU_LIBLISTSZ),
351 QUERY(DT_CHECKSUM),
352 QUERY(DT_PLTPADSZ),
353 QUERY(DT_MOVEENT),
354 QUERY(DT_MOVESZ),
355 QUERY(DT_ADDRRNGLO),
356 QUERY(DT_GNU_HASH),
357 QUERY(DT_TLSDESC_PLT),
358 QUERY(DT_TLSDESC_GOT),
359 QUERY(DT_GNU_CONFLICT),
360 QUERY(DT_GNU_LIBLIST),
361 QUERY(DT_CONFIG),
362 QUERY(DT_DEPAUDIT),
363 QUERY(DT_AUDIT),
364 { 0, 0 }
365 };
366 const char *get_elfdtype(int type)
367 {
368 return find_pairtype(elf_dtypes, type);
369 }
370
371 /* translate elf SHT_ defines */
372 static pairtype elf_shttypes[] = {
373 QUERY(SHT_NULL),
374 QUERY(SHT_PROGBITS),
375 QUERY(SHT_SYMTAB),
376 QUERY(SHT_STRTAB),
377 QUERY(SHT_RELA),
378 QUERY(SHT_HASH),
379 QUERY(SHT_DYNAMIC),
380 QUERY(SHT_NOTE),
381 QUERY(SHT_NOBITS),
382 QUERY(SHT_REL),
383 QUERY(SHT_SHLIB),
384 QUERY(SHT_DYNSYM),
385 QUERY(SHT_INIT_ARRAY),
386 QUERY(SHT_FINI_ARRAY),
387 QUERY(SHT_PREINIT_ARRAY),
388 QUERY(SHT_GROUP),
389 QUERY(SHT_SYMTAB_SHNDX),
390 QUERY(SHT_GNU_ATTRIBUTES),
391 QUERY(SHT_GNU_HASH),
392 QUERY(SHT_GNU_LIBLIST),
393 QUERY(SHT_CHECKSUM),
394 QUERY(SHT_SUNW_move),
395 QUERY(SHT_SUNW_COMDAT),
396 QUERY(SHT_SUNW_syminfo),
397 QUERY(SHT_GNU_verdef),
398 QUERY(SHT_GNU_verneed),
399 QUERY(SHT_GNU_versym),
400 { 0, 0 }
401 };
402 const char *get_elfshttype(int type)
403 {
404 return find_pairtype(elf_shttypes, type);
405 }
406
407 /* translate elf STT_ defines */
408 static pairtype elf_stttypes[] = {
409 QUERY(STT_NOTYPE),
410 QUERY(STT_OBJECT),
411 QUERY(STT_FUNC),
412 QUERY(STT_SECTION),
413 QUERY(STT_FILE),
414 QUERY(STT_COMMON),
415 QUERY(STT_TLS),
416 QUERY(STT_GNU_IFUNC),
417 { 0, 0 }
418 };
419 const char *get_elfstttype(int type)
420 {
421 return find_pairtype(elf_stttypes, type);
422 }
423
424 /* translate elf STB_ defines */
425 static pairtype elf_stbtypes[] = {
426 QUERY(STB_LOCAL),
427 QUERY(STB_GLOBAL),
428 QUERY(STB_WEAK),
429 QUERY(STB_GNU_UNIQUE),
430 { 0, 0 }
431 };
432 const char *get_elfstbtype(int type)
433 {
434 return find_pairtype(elf_stbtypes, type);
435 }
436
437 /* translate elf SHN_ defines */
438 static pairtype elf_shntypes[] = {
439 QUERY(SHN_UNDEF),
440 QUERY(SHN_BEFORE),
441 QUERY(SHN_AFTER),
442 QUERY(SHN_ABS),
443 QUERY(SHN_COMMON),
444 { 0, 0 }
445 };
446 const char *get_elfshntype(int type)
447 {
448 if (type && type < SHN_LORESERVE)
449 return "DEFINED";
450 return find_pairtype(elf_shntypes, type);
451 }
452
453 /* Read an ELF into memory */
454 #define IS_ELF_BUFFER(buff) \
455 (buff[EI_MAG0] == ELFMAG0 && \
456 buff[EI_MAG1] == ELFMAG1 && \
457 buff[EI_MAG2] == ELFMAG2 && \
458 buff[EI_MAG3] == ELFMAG3)
459 #define DO_WE_LIKE_ELF(buff) \
460 ((buff[EI_CLASS] == ELFCLASS32 || buff[EI_CLASS] == ELFCLASS64) && \
461 (buff[EI_DATA] == ELFDATA2LSB || buff[EI_DATA] == ELFDATA2MSB) && \
462 (buff[EI_VERSION] == EV_CURRENT))
463 elfobj *readelf_buffer(const char *filename, void *buffer, size_t buffer_len)
464 {
465 elfobj *elf;
466
467 /* make sure we have enough bytes to scan e_ident */
468 if (buffer == NULL || buffer_len < EI_NIDENT)
469 return NULL;
470
471 elf = xzalloc(sizeof(*elf));
472
473 elf->fd = -1;
474 elf->len = buffer_len;
475 elf->data = buffer;
476 elf->data_end = buffer + buffer_len;
477
478 /* make sure we have an elf */
479 if (!IS_ELF_BUFFER(elf->data)) {
480 free_elf_and_return:
481 free(elf);
482 return NULL;
483 }
484
485 /* check class and stuff */
486 if (!DO_WE_LIKE_ELF(elf->data)) {
487 warn("we no likey %s: {%s,%s,%s,%s}",
488 filename,
489 get_elfeitype(EI_CLASS, elf->data[EI_CLASS]),
490 get_elfeitype(EI_DATA, elf->data[EI_DATA]),
491 get_elfeitype(EI_VERSION, elf->data[EI_VERSION]),
492 get_elfeitype(EI_OSABI, elf->data[EI_OSABI]));
493 goto free_elf_and_return;
494 }
495
496 elf->filename = filename;
497 elf->base_filename = strrchr(filename, '/');
498 if (elf->base_filename == NULL)
499 elf->base_filename = elf->filename;
500 else
501 elf->base_filename = elf->base_filename + 1;
502 elf->elf_class = elf->data[EI_CLASS];
503 do_reverse_endian = (ELF_DATA != elf->data[EI_DATA]);
504
505 /* for arches that need alignment, we have to make sure the buffer
506 * is strictly aligned. archive (.a) files only align to 2 bytes
507 * while the arch can easily require 8. so dupe the buffer so
508 * that our local copy is always aligned (since we can't shift the
509 * file mapping back and forth a few bytes).
510 */
511 if (!__PAX_UNALIGNED_OK && ((unsigned long)elf->vdata & 0x7)) {
512 elf->_data = xmalloc(elf->len);
513 memcpy(elf->_data, elf->data, elf->len);
514 elf->data = elf->_data;
515 elf->data_end = elf->_data + elf->len;
516 }
517
518 #define READELF_HEADER(B) \
519 if (elf->elf_class == ELFCLASS ## B) { \
520 char invalid; \
521 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
522 Elf ## B ## _Off size; \
523 /* verify program header */ \
524 invalid = 0; \
525 if (EGET(ehdr->e_phnum) <= 0) \
526 invalid = 1; /* this is not abnormal so dont warn */ \
527 else if (EGET(ehdr->e_phentsize) != sizeof(Elf ## B ## _Phdr)) \
528 invalid = 3; \
529 else { \
530 elf->phdr = elf->vdata + EGET(ehdr->e_phoff); \
531 size = EGET(ehdr->e_phnum) * EGET(ehdr->e_phentsize); \
532 if (elf->phdr < elf->ehdr || /* check overflow */ \
533 elf->phdr + size < elf->phdr || /* before start of mem */ \
534 elf->phdr + size > elf->ehdr + elf->len) /* before end of mem */ \
535 invalid = 2; \
536 } \
537 if (invalid > 1) \
538 warn("%s: Invalid program header info (%i)", filename, invalid); \
539 if (invalid) \
540 elf->phdr = NULL; \
541 /* verify section header */ \
542 invalid = 0; \
543 if (EGET(ehdr->e_shnum) <= 0) \
544 invalid = 1; /* this is not abnormal so dont warn */ \
545 else if (EGET(ehdr->e_shentsize) != sizeof(Elf ## B ## _Shdr)) \
546 invalid = 3; \
547 else { \
548 elf->shdr = elf->vdata + EGET(ehdr->e_shoff); \
549 size = EGET(ehdr->e_shnum) * EGET(ehdr->e_shentsize); \
550 if (elf->shdr < elf->ehdr || /* check overflow */ \
551 elf->shdr + size < elf->shdr || /* before start of mem */ \
552 elf->shdr + size > elf->ehdr + elf->len) /* before end of mem */ \
553 invalid = 2; \
554 } \
555 if (invalid > 1) \
556 warn("%s: Invalid section header info (%i)", filename, invalid); \
557 if (invalid) \
558 elf->shdr = NULL; \
559 }
560 READELF_HEADER(32)
561 READELF_HEADER(64)
562 /* { char *p; strncpy(elf->basename, (p = strrchr(filename, '/')) == NULL ? "?" : p+1 , sizeof(elf->basename)); } */
563
564 return elf;
565 }
566 elfobj *_readelf_fd(const char *filename, int fd, size_t len, int read_only)
567 {
568 char *buffer;
569 elfobj *ret;
570
571 if (len == 0) {
572 struct stat st;
573 if (fstat(fd, &st) == -1)
574 return NULL;
575 len = st.st_size;
576 if (len == 0)
577 return NULL;
578 }
579
580 buffer = mmap(0, len, PROT_READ | (read_only ? 0 : PROT_WRITE), (read_only ? MAP_PRIVATE : MAP_SHARED), fd, 0);
581 if (buffer == MAP_FAILED) {
582 warn("mmap on '%s' of %li bytes failed :(", filename, (unsigned long)len);
583 return NULL;
584 }
585
586 ret = readelf_buffer(filename, buffer, len);
587 if (ret == NULL)
588 munmap(buffer, len);
589 else {
590 ret->fd = fd;
591 ret->is_mmap = 1;
592 }
593
594 return ret;
595 }
596 elfobj *_readelf(const char *filename, int read_only)
597 {
598 elfobj *ret;
599 struct stat st;
600 int fd;
601
602 if (stat(filename, &st) == -1)
603 return NULL;
604
605 if ((fd = open(filename, (read_only ? O_RDONLY : O_RDWR))) == -1)
606 return NULL;
607
608 /* make sure we have enough bytes to scan e_ident */
609 if (st.st_size <= EI_NIDENT) {
610 close_fd_and_return:
611 close(fd);
612 return NULL;
613 }
614
615 ret = readelf_fd(filename, fd, st.st_size);
616 if (ret == NULL)
617 goto close_fd_and_return;
618
619 return ret;
620 }
621
622 /* undo the readelf() stuff */
623 void unreadelf(elfobj *elf)
624 {
625 if (elf->is_mmap) munmap(elf->vdata, elf->len);
626 if (elf->fd != -1) close(elf->fd);
627 if (!__PAX_UNALIGNED_OK) free(elf->_data);
628 free(elf);
629 }
630
631 char *pax_short_hf_flags(unsigned long flags)
632 {
633 static char buffer[7];
634
635 buffer[0] = (flags & HF_PAX_PAGEEXEC ? 'p' : 'P');
636 buffer[1] = (flags & HF_PAX_EMUTRAMP ? 'E' : 'e');
637 buffer[2] = (flags & HF_PAX_MPROTECT ? 'm' : 'M');
638 buffer[3] = (flags & HF_PAX_RANDMMAP ? 'r' : 'R');
639 buffer[4] = (flags & HF_PAX_RANDEXEC ? 'X' : 'x');
640 buffer[5] = (flags & HF_PAX_SEGMEXEC ? 's' : 'S');
641 buffer[6] = 0;
642
643 return buffer;
644 }
645
646 /* PT_PAX_FLAGS are tristate ...
647 * the display logic is:
648 * lower case: explicitly disabled
649 * upper case: explicitly enabled
650 * - : default */
651 char *pax_short_pf_flags(unsigned long flags)
652 {
653 static char buffer[7];
654
655 #define PAX_STATE(pf_on, pf_off, disp_on, disp_off) \
656 (flags & pf_on ? disp_on : (flags & pf_off ? disp_off : '-'))
657
658 buffer[0] = PAX_STATE(PF_PAGEEXEC, PF_NOPAGEEXEC, 'P', 'p');
659 buffer[1] = PAX_STATE(PF_SEGMEXEC, PF_NOSEGMEXEC, 'S', 's');
660 buffer[2] = PAX_STATE(PF_MPROTECT, PF_NOMPROTECT, 'M', 'm');
661 buffer[3] = PAX_STATE(PF_RANDEXEC, PF_NORANDEXEC, 'X', 'x');
662 buffer[4] = PAX_STATE(PF_EMUTRAMP, PF_NOEMUTRAMP, 'E', 'e');
663 buffer[5] = PAX_STATE(PF_RANDMMAP, PF_NORANDMMAP, 'R', 'r');
664 buffer[6] = 0;
665
666 if (((flags & PF_PAGEEXEC) && (flags & PF_NOPAGEEXEC)) || \
667 ((flags & PF_SEGMEXEC) && (flags & PF_NOSEGMEXEC)) || \
668 ((flags & PF_RANDMMAP) && (flags & PF_NORANDMMAP)) || \
669 ((flags & PF_RANDEXEC) && (flags & PF_NORANDEXEC)) || \
670 ((flags & PF_EMUTRAMP) && (flags & PF_NOEMUTRAMP)) || \
671 ((flags & PF_RANDMMAP) && (flags & PF_NORANDMMAP)))
672 warn("inconsistent state detected. flags=%lX\n", flags);
673
674 return buffer;
675 }
676
677 char *gnu_short_stack_flags(unsigned long flags)
678 {
679 static char buffer[4];
680
681 buffer[0] = (flags & PF_R ? 'R' : '-');
682 buffer[1] = (flags & PF_W ? 'W' : '-');
683 buffer[2] = (flags & PF_X ? 'X' : '-');
684 buffer[3] = 0;
685
686 return buffer;
687 }
688
689 void *elf_findsecbyname(elfobj *elf, const char *name)
690 {
691 unsigned int i;
692 char *shdr_name;
693 void *ret = NULL;
694
695 if (elf->shdr == NULL) return NULL;
696
697 #define FINDSEC(B) \
698 if (elf->elf_class == ELFCLASS ## B) { \
699 Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
700 Elf ## B ## _Shdr *shdr = SHDR ## B (elf->shdr); \
701 Elf ## B ## _Shdr *strtbl; \
702 Elf ## B ## _Off offset; \
703 uint16_t shstrndx = EGET(ehdr->e_shstrndx); \
704 uint16_t shnum = EGET(ehdr->e_shnum); \
705 if (shstrndx >= shnum) return NULL; \
706 strtbl = &(shdr[shstrndx]); \
707 for (i = 0; i < shnum; ++i) { \
708 offset = EGET(strtbl->sh_offset) + EGET(shdr[i].sh_name); \
709 if (offset >= (Elf ## B ## _Off)elf->len) continue; \
710 shdr_name = elf->data + offset; \
711 if (!strcmp(shdr_name, name)) { \
712 if (ret) warnf("Multiple '%s' sections !?", name); \
713 ret = (void*)&(shdr[i]); \
714 } \
715 } }
716 FINDSEC(32)
717 FINDSEC(64)
718
719 return ret;
720 }
721
722 #if 0
723 # define ELFOSABI_NONE 0 /* UNIX System V ABI */
724 # define ELFOSABI_SYSV 0 /* Alias. */
725 # define ELFOSABI_HPUX 1 /* HP-UX */
726 # define ELFOSABI_NETBSD 2 /* NetBSD. */
727 # define ELFOSABI_LINUX 3 /* Linux. */
728 # define ELFOSABI_SOLARIS 6 /* Sun Solaris. */
729 # define ELFOSABI_AIX 7 /* IBM AIX. */
730 # define ELFOSABI_IRIX 8 /* SGI Irix. */
731 # define ELFOSABI_FREEBSD 9 /* FreeBSD. */
732 # define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */
733 # define ELFOSABI_MODESTO 11 /* Novell Modesto. */
734 # define ELFOSABI_OPENBSD 12 /* OpenBSD. */
735 # define ELFOSABI_ARM 97 /* ARM */
736 # define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */
737
738 /* These 3 ABIs should be in elf.h but are not.
739 * http://www.caldera.com/developers/gabi/latest/ch4.eheader.html#generic_osabi_values
740 */
741
742 # define ELFOSABI_OPENVMS 13 /* OpenVMS */
743 # define ELFOSABI_NSK 14 /* Hewlett-Packard Non-Stop Kernel */
744 # define ELFOSABI_AROS 15 /* Amiga Research OS */
745
746 #4 reserved for IA32 GNU Mach/Hurd
747 #5 reserved for 86Open common IA32 ABI
748
749 #endif

  ViewVC Help
Powered by ViewVC 1.1.20