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

Contents of /pax-utils/paxelf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.74 - (show annotations) (download) (as text)
Sun Nov 18 07:39:45 2012 UTC (17 months ago) by vapier
Branch: MAIN
Changes since 1.73: +1 -19 lines
File MIME type: text/x-csrc
scanelf/pspax: drop PT_LOAD counts since more than "normal" is not a bug and is semi-common with some targets, and the warning has out lived its usefulness -- it was added as an initial sanity check to get a feel for the real world

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

  ViewVC Help
Powered by ViewVC 1.1.20