diff options
author | jkoshy <jkoshy@FreeBSD.org> | 2006-12-25 02:22:22 +0000 |
---|---|---|
committer | jkoshy <jkoshy@FreeBSD.org> | 2006-12-25 02:22:22 +0000 |
commit | 26d8253ac6532abcb8d1c8d57743998f5368173b (patch) | |
tree | 1f1c4db9c6b866adf56e5cc3eccd23baaa57af28 /lib/libelf/elf_shstrndx.c | |
parent | 44313eba34c379a180dc73dcd02ab9c415941db3 (diff) | |
download | FreeBSD-src-26d8253ac6532abcb8d1c8d57743998f5368173b.zip FreeBSD-src-26d8253ac6532abcb8d1c8d57743998f5368173b.tar.gz |
Keep shadow copies of the `e_shnum', `e_phnum' and `e_shstrndx'
members of the ELF Executable Header inside the library-private
`struct _Elf' descriptor and only update the underlying Elf{32,64}_Ehdr
structure on an elf_update(3) call. These fields of the Ehdr
structure are technically `out of bounds' for an application program
per the ELF(3) API, but we've seen applications that initialize
a new Ehdr structure using memcpy(), messing up the library's
invariants. [1]
Implement elf_getphnum() and handle ELF objects with more than
64K program header table entries.
Reported by: jb [1]
Diffstat (limited to 'lib/libelf/elf_shstrndx.c')
-rw-r--r-- | lib/libelf/elf_shstrndx.c | 72 |
1 files changed, 7 insertions, 65 deletions
diff --git a/lib/libelf/elf_shstrndx.c b/lib/libelf/elf_shstrndx.c index cae25c3..f4f502d 100644 --- a/lib/libelf/elf_shstrndx.c +++ b/lib/libelf/elf_shstrndx.c @@ -32,68 +32,6 @@ __FBSDID("$FreeBSD$"); #include "_libelf.h" -/* - * Helpers to get/set the e_shstrndx field of the ELF header. - */ - -int -_libelf_getshstrndx(Elf *e, void *eh, int ec, size_t *strndx) -{ - Elf_Scn *scn; - void *sh; - size_t n; - - n = (ec == ELFCLASS32) ? ((Elf32_Ehdr *) eh)->e_shstrndx : - ((Elf64_Ehdr *) eh)->e_shstrndx; - - if (n < SHN_LORESERVE) { - *strndx = n; - return (1); - } - - if ((scn = elf_getscn(e, (size_t) SHN_UNDEF)) == NULL) - return (0); - if ((sh = _libelf_getshdr(scn, ec)) == NULL) - return (0); - - if (ec == ELFCLASS32) - *strndx = ((Elf32_Shdr *) sh)->sh_link; - else - *strndx = ((Elf64_Shdr *) sh)->sh_link; - - return (1); -} - -int -_libelf_setshstrndx(Elf *e, void *eh, int ec, size_t strndx) -{ - Elf_Scn *scn; - void *sh; - - if (strndx < SHN_LORESERVE) { - if (ec == ELFCLASS32) - ((Elf32_Ehdr *) eh)->e_shstrndx = strndx; - else - ((Elf64_Ehdr *) eh)->e_shstrndx = strndx; - return (1); - } - - if ((scn = elf_getscn(e, (size_t) SHN_UNDEF)) == NULL) - return (0); - if ((sh = _libelf_getshdr(scn, ec)) == NULL) - return (0); - - if (ec == ELFCLASS32) { - ((Elf32_Ehdr *) eh)->e_shstrndx = SHN_XINDEX; - ((Elf32_Shdr *) sh)->sh_link = strndx; - } else { - ((Elf64_Ehdr *) eh)->e_shstrndx = SHN_XINDEX; - ((Elf64_Shdr *) sh)->sh_link = strndx; - } - - return (1); -} - int elf_getshstrndx(Elf *e, size_t *strndx) { @@ -101,13 +39,17 @@ elf_getshstrndx(Elf *e, size_t *strndx) int ec; if (e == NULL || e->e_kind != ELF_K_ELF || - ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) || - ((eh = _libelf_ehdr(e, ec, 0)) == NULL)) { + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { LIBELF_SET_ERROR(ARGUMENT, 0); return (0); } - return (_libelf_getshstrndx(e, eh, ec, strndx)); + if ((eh = _libelf_ehdr(e, ec, 0)) == NULL) + return (0); + + *strndx = e->e_u.e_elf.e_strndx; + + return (1); } int |