diff options
author | Mike Frysinger <vapier@gentoo.org> | 2005-04-05 00:51:33 +0000 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2005-04-05 00:51:33 +0000 |
commit | 01056e991a2b90a7126adff4ece9e0fcbc801f57 (patch) | |
tree | b07a968d2741878e7000852a409a4c39482b9b93 /paxelf.h | |
parent | add basic sanity check if user didnt give us any paths to scan (diff) | |
download | pax-utils-01056e991a2b90a7126adff4ece9e0fcbc801f57.tar.gz pax-utils-01056e991a2b90a7126adff4ece9e0fcbc801f57.tar.bz2 pax-utils-01056e991a2b90a7126adff4ece9e0fcbc801f57.zip |
handle any bitsize / endian type
Diffstat (limited to 'paxelf.h')
-rw-r--r-- | paxelf.h | 80 |
1 files changed, 54 insertions, 26 deletions
@@ -9,6 +9,7 @@ #ifdef __linux__ # include <elf.h> # include <asm/elf.h> +# include <byteswap.h> #else # include <sys/elf_common.h> #endif @@ -17,30 +18,51 @@ # error "UNABLE TO DETECT ELF_CLASS" #endif -/* we need to hide bitsize (32/64) and endian (little/big) */ - -#if (ELF_CLASS == ELFCLASS32) -# define Elf_Ehdr Elf32_Ehdr -# define Elf_Phdr Elf32_Phdr -# define Elf_Shdr Elf32_Shdr -# define Elf_Dyn Elf32_Dyn -#endif - -#if (ELF_CLASS == ELFCLASS64) -# define Elf_Ehdr Elf64_Ehdr -# define Elf_Phdr Elf64_Phdr -# define Elf_Shdr Elf64_Shdr -# define Elf_Dyn Elf64_Dyn -#endif +extern char do_reverse_endian; +/* Get a value 'X' in the elf header, compensating for endianness. */ +#define EGET(X) \ + (__extension__ ({ \ + uint64_t __res; \ + if (!do_reverse_endian) { __res = (X); \ + } else if (sizeof(X) == 1) { __res = (X); \ + } else if (sizeof(X) == 2) { __res = bswap_16((X)); \ + } else if (sizeof(X) == 4) { __res = bswap_32((X)); \ + } else if (sizeof(X) == 8) { __res = bswap_64((X)); \ + } else { \ + fprintf(stderr, "EGET failed ;(\n"); \ + exit(EXIT_FAILURE); \ + } \ + __res; \ + })) +/* Set a value 'Y' in the elf header to 'X', compensating for endianness. */ +#define ESET(Y,X) \ + do if (!do_reverse_endian) { Y = (X); \ + } else if (sizeof(Y) == 1) { Y = (X); \ + } else if (sizeof(Y) == 2) { Y = bswap_16((uint16_t)(X)); \ + } else if (sizeof(Y) == 4) { Y = bswap_32((uint32_t)(X)); \ + } else if (sizeof(Y) == 8) { Y = bswap_64((uint64_t)(X)); \ + } else { \ + fprintf(stderr, "ESET failed ;(\n")); \ + exit(EXIT_FAILURE); \ + } while (0) typedef struct { - Elf_Ehdr *ehdr; - Elf_Phdr *phdr; - Elf_Shdr *shdr; + void *ehdr; + void *phdr; + void *shdr; char *data; + char elf_class; int len; int fd; } elfobj; +#define EHDR32(ptr) ((Elf32_Ehdr *)(ptr)) +#define EHDR64(ptr) ((Elf64_Ehdr *)(ptr)) +#define PHDR32(ptr) ((Elf32_Phdr *)(ptr)) +#define PHDR64(ptr) ((Elf64_Phdr *)(ptr)) +#define SHDR32(ptr) ((Elf32_Shdr *)(ptr)) +#define SHDR64(ptr) ((Elf64_Shdr *)(ptr)) +#define DYN32(ptr) ((Elf32_Dyn *)(ptr)) +#define DYN64(ptr) ((Elf64_Dyn *)(ptr)) /* prototypes */ extern char *pax_short_hf_flags(unsigned long flags); @@ -48,15 +70,11 @@ extern char *pax_short_pf_flags(unsigned long flags); extern char *gnu_short_stack_flags(unsigned long flags); extern elfobj *readelf(const char *filename); extern void unreadelf(elfobj *elf); -extern const char *get_elfetype(int type); +extern const char *get_elfeitype(elfobj *elf, int ei_type, int type); +extern const char *get_elfetype(elfobj *elf); extern const char *get_elfptype(int type); extern const char *get_elfdtype(int type); -extern const char *elf_getsecname(elfobj *elf, Elf_Shdr *shdr); -extern Elf_Shdr *elf_findsecbyname(elfobj *elf, const char *name); - -#define IS_ELF_TYPE(elf, type) (elf->ehdr->e_type == type) -#define IS_ELF_ET_EXEC(elf) IS_ELF_TYPE(elf, ET_EXEC) -#define IS_ELF_ET_DYN(elf) IS_ELF_TYPE(elf, ET_DYN) +extern void *elf_findsecbyname(elfobj *elf, const char *name); /* PaX flags (to be read in elfhdr.e_flags) */ #define HF_PAX_PAGEEXEC 1 /* 0: Paging based non-exec pages */ @@ -67,7 +85,17 @@ extern Elf_Shdr *elf_findsecbyname(elfobj *elf, const char *name); #define HF_PAX_SEGMEXEC 32 /* 0: Segmentation based non-exec pages */ #define EI_PAX 14 /* Index in e_ident[] where to read flags */ -#define PAX_FLAGS(elf) ((elf->ehdr->e_ident[EI_PAX + 1] << 8) + (elf->ehdr->e_ident[EI_PAX])) +#define __PAX_FLAGS(B, elf) \ + ((EHDR ## B (elf->ehdr)->e_ident[EI_PAX + 1] << 8) + EHDR ## B (elf->ehdr)->e_ident[EI_PAX]) +#define PAX_FLAGS(elf) \ + (__extension__ ({ \ + unsigned long __res; \ + if (elf->elf_class == ELFCLASS32) \ + __res = __PAX_FLAGS(32, elf); \ + else \ + __res = __PAX_FLAGS(64, elf); \ + __res; \ + })) /* * in case we are not defined by proper/up-to-date system headers, |