├── .gitignore ├── COPYING.LIB ├── ChangeLog ├── INSTALL ├── MANIFEST ├── Makefile.in ├── README ├── Unix ├── .gitignore └── Makefile ├── VC ├── .gitignore ├── check.c.w32 ├── libelf.def ├── libelf.sln ├── libelf.vcproj └── prebuild.bat ├── VERSION ├── acconfig.h ├── aclocal.m4 ├── config.guess ├── config.h.in ├── config.sub ├── configure ├── configure.in ├── install-sh ├── lib ├── .gitignore ├── 32.fsize.c ├── 32.getehdr.c ├── 32.getphdr.c ├── 32.getshdr.c ├── 32.newehdr.c ├── 32.newphdr.c ├── 32.xlatetof.c ├── 64.xlatetof.c ├── Makefile.in ├── Makefile.w32 ├── assert.c ├── begin.c ├── build.bat ├── byteswap.h ├── checksum.c ├── cntl.c ├── config.h.w32 ├── cook.c ├── data.c ├── elf_repl.h ├── end.c ├── errmsg.c ├── errno.c ├── errors.h ├── ext_types.h ├── fill.c ├── flag.c ├── gelf.h ├── gelfehdr.c ├── gelfphdr.c ├── gelfshdr.c ├── gelftrans.c ├── getarhdr.c ├── getarsym.c ├── getbase.c ├── getdata.c ├── getident.c ├── getscn.c ├── hash.c ├── input.c ├── kind.c ├── libelf.def ├── libelf.h ├── memset.c ├── ndxscn.c ├── newdata.c ├── newscn.c ├── next.c ├── nextscn.c ├── nlist.c ├── nlist.h ├── opt.delscn.c ├── private.h ├── rand.c ├── rawdata.c ├── rawfile.c ├── strptr.c ├── swap64.c ├── sys_elf.h.in ├── sys_elf.h.w32 ├── update.c ├── verdef.h ├── verdef_32_tof.c ├── verdef_32_tom.c ├── verdef_64_tof.c ├── verdef_64_tom.c ├── verneed.h ├── version.c ├── x.elfext.c ├── x.movscn.c └── x.remscn.c ├── libelf.pc.in ├── mkinstalldirs ├── po ├── Makefile.in ├── de.gmo ├── de.msg ├── de.po ├── gmo2msg.c ├── libelf.pot └── stamp-po └── stamp-h.in /.gitignore: -------------------------------------------------------------------------------- 1 | Makefile 2 | config.cache 3 | config.h 4 | config.log 5 | config.status 6 | stamp-h 7 | libelf.pc 8 | 9 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | 25275 COPYING.LIB 2 | 35032 ChangeLog 3 | 7462 INSTALL 4 | 5908 Makefile.in 5 | 13159 README 6 | 7 VERSION 7 | 2955 acconfig.h 8 | 9381 aclocal.m4 9 | 43611 config.guess 10 | 3987 config.h.in 11 | 31160 config.sub 12 | 110753 configure 13 | 11736 configure.in 14 | 2186 install-sh 15 | 4798 lib/32.fsize.c 16 | 1556 lib/32.getehdr.c 17 | 1557 lib/32.getphdr.c 18 | 1665 lib/32.getshdr.c 19 | 2181 lib/32.newehdr.c 20 | 3055 lib/32.newphdr.c 21 | 12322 lib/32.xlatetof.c 22 | 14221 lib/64.xlatetof.c 23 | 7990 lib/Makefile.in 24 | 3999 lib/Makefile.w32 25 | 1119 lib/assert.c 26 | 10795 lib/begin.c 27 | 1685 lib/build.bat 28 | 3566 lib/byteswap.h 29 | 3937 lib/checksum.c 30 | 1901 lib/cntl.c 31 | 4851 lib/config.h.w32 32 | 12554 lib/cook.c 33 | 1183 lib/data.c 34 | 24544 lib/elf_repl.h 35 | 2899 lib/end.c 36 | 2137 lib/errmsg.c 37 | 1037 lib/errno.c 38 | 6222 lib/errors.h 39 | 8107 lib/ext_types.h 40 | 997 lib/fill.c 41 | 2400 lib/flag.c 42 | 5103 lib/gelf.h 43 | 4151 lib/gelfehdr.c 44 | 3965 lib/gelfphdr.c 45 | 3758 lib/gelfshdr.c 46 | 9456 lib/gelftrans.c 47 | 1175 lib/getarhdr.c 48 | 2421 lib/getarsym.c 49 | 1096 lib/getbase.c 50 | 3841 lib/getdata.c 51 | 1353 lib/getident.c 52 | 1449 lib/getscn.c 53 | 1212 lib/hash.c 54 | 2562 lib/input.c 55 | 1088 lib/kind.c 56 | 1214 lib/libelf.def 57 | 9050 lib/libelf.h 58 | 1505 lib/memset.c 59 | 1098 lib/ndxscn.c 60 | 1558 lib/newdata.c 61 | 3488 lib/newscn.c 62 | 1356 lib/next.c 63 | 1607 lib/nextscn.c 64 | 5898 lib/nlist.c 65 | 1452 lib/nlist.h 66 | 5071 lib/opt.delscn.c 67 | 12801 lib/private.h 68 | 1286 lib/rand.c 69 | 2543 lib/rawdata.c 70 | 1541 lib/rawfile.c 71 | 3393 lib/strptr.c 72 | 2285 lib/swap64.c 73 | 3812 lib/sys_elf.h.in 74 | 4066 lib/sys_elf.h.w32 75 | 26008 lib/update.c 76 | 6884 lib/verdef.h 77 | 1586 lib/verdef_32_tof.c 78 | 1586 lib/verdef_32_tom.c 79 | 1614 lib/verdef_64_tof.c 80 | 1614 lib/verdef_64_tom.c 81 | 7135 lib/verneed.h 82 | 1435 lib/version.c 83 | 4425 lib/x.elfext.c 84 | 2721 lib/x.movscn.c 85 | 2870 lib/x.remscn.c 86 | 238 libelf.pc.in 87 | 619 mkinstalldirs 88 | 4593 po/Makefile.in 89 | 8748 po/de.gmo 90 | 52 po/de.msg 91 | 11332 po/de.po 92 | 3011 po/gmo2msg.c 93 | 6483 po/libelf.pot 94 | 10 po/stamp-po 95 | 10 stamp-h.in 96 | -------------------------------------------------------------------------------- /Makefile.in: -------------------------------------------------------------------------------- 1 | # Makefile for libelf. 2 | # Copyright (C) 1995 - 2005 Michael Riepe 3 | # 4 | # This library is free software; you can redistribute it and/or 5 | # modify it under the terms of the GNU Library General Public 6 | # License as published by the Free Software Foundation; either 7 | # version 2 of the License, or (at your option) any later version. 8 | # 9 | # This library is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | # Library General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU Library General Public 15 | # License along with this library; if not, write to the Free Software 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 17 | 18 | # @(#) $Id: Makefile.in,v 1.30 2008/05/23 08:17:56 michael Exp $ 19 | 20 | instroot = 21 | 22 | prefix = @prefix@ 23 | exec_prefix = @exec_prefix@ 24 | libdir = @libdir@ 25 | 26 | pkgdir = $(libdir)/pkgconfig 27 | 28 | MV = mv -f 29 | RM = rm -f 30 | LN_S = @LN_S@ 31 | INSTALL = @INSTALL@ 32 | INSTALL_DATA = @INSTALL_DATA@ 33 | 34 | CC = @CC@ 35 | CFLAGS = @CFLAGS@ 36 | CPPFLAGS = @CPPFLAGS@ 37 | LDFLAGS = @LDFLAGS@ 38 | LIBS = @LIBS@ 39 | 40 | # no user serviceable parts below 41 | 42 | PACKAGE = @PACKAGE@ 43 | VERSION = @VERSION@ 44 | 45 | SHELL = /bin/sh 46 | @SET_MAKE@ 47 | 48 | srcdir = @srcdir@ 49 | VPATH = @srcdir@ 50 | 51 | SUBDIRS = lib @POSUB@ 52 | DISTSUBDIRS = lib po 53 | 54 | DISTFILES = \ 55 | acconfig.h aclocal.m4 ChangeLog config.guess config.h.in \ 56 | config.sub configure configure.in COPYING.LIB INSTALL install-sh \ 57 | Makefile.in mkinstalldirs README stamp-h.in VERSION libelf.pc.in 58 | 59 | all: all-recursive all-local 60 | check: check-recursive check-local 61 | install: install-recursive install-local 62 | uninstall: uninstall-recursive uninstall-local 63 | mostlyclean: mostlyclean-recursive mostlyclean-local 64 | clean: clean-recursive clean-local 65 | distclean: distclean-recursive distclean-local 66 | maintainer-clean: maintainer-clean-recursive maintainer-clean-local 67 | 68 | install-compat uninstall-compat: 69 | cd lib && $(MAKE) $@ 70 | 71 | all-recursive check-recursive install-recursive uninstall-recursive \ 72 | clean-recursive distclean-recursive mostlyclean-recursive \ 73 | maintainer-clean-recursive: 74 | @subdirs="$(SUBDIRS)"; for subdir in $$subdirs; do \ 75 | target=`echo $@|sed 's,-recursive,,'`; \ 76 | echo making $$target in $$subdir; \ 77 | (cd $$subdir && $(MAKE) $$target) || exit 1; \ 78 | done 79 | 80 | all-local: 81 | 82 | check-local: 83 | 84 | install-local: $(srcdir)/mkinstalldirs libelf.pc 85 | $(SHELL) $(srcdir)/mkinstalldirs $(instroot)$(pkgdir) 86 | $(INSTALL_DATA) libelf.pc $(instroot)$(pkgdir) 87 | 88 | uninstall-local: 89 | $(RM) $(instroot)$(pkgdir)/libelf.pc 90 | 91 | mostlyclean-local: 92 | $(RM) *~ core errlist 93 | 94 | clean-local: mostlyclean-local 95 | 96 | distclean-local: clean-local 97 | $(RM) config.cache config.h config.log config.status stamp-h 98 | $(RM) Makefile 99 | $(RM) libelf.pc 100 | 101 | maintainer-clean-local: distclean-local 102 | @echo "This command is intended for maintainers to use;" 103 | @echo "it deletes files that may require special tools to rebuild." 104 | $(RM) config.h.in configure stamp-dist 105 | $(RM) -r $(distdir) 106 | 107 | # maintainer only 108 | 109 | MAINT = @MAINT@ 110 | 111 | distdir = $(PACKAGE)-$(VERSION) 112 | DISTPERMS = --owner=root --group=root --numeric-owner 113 | $(MAINT)dist: ./stamp-dist 114 | $(MAINT)./stamp-dist: $(DISTFILES) 115 | $(RM) -r $(distdir) 116 | mkdir $(distdir) 117 | files="$(DISTFILES)"; for file in $$files; do \ 118 | ln $(srcdir)/$$file $(distdir) || \ 119 | cp -p $(srcdir)/$$file $(distdir) || exit 1; \ 120 | done 121 | subdirs="$(DISTSUBDIRS)"; for subdir in $$subdirs; do \ 122 | (cd $$subdir && $(MAKE) dist) || exit 1; \ 123 | done 124 | cd $(distdir) && \ 125 | find . -type f ! -name MANIFEST -exec wc -c {} \; | \ 126 | sed 's, \./, ,' | sort -k2 >MANIFEST 127 | -$(RM) $(distdir).tar.gz.bak $(PACKAGE).tar.gz 128 | -$(MV) $(distdir).tar.gz $(distdir).tar.gz.bak 129 | tar cvohfz $(distdir).tar.gz $(DISTPERMS) $(distdir) 130 | $(LN_S) $(distdir).tar.gz $(PACKAGE).tar.gz 131 | $(RM) stamp-dist && echo timestamp > stamp-dist 132 | 133 | $(MAINT)check-dist: 134 | $(RM) -r disttest 135 | mkdir disttest 136 | @echo 'unset CC CFLAGS CPPFLAGS LDFLAGS LIBS' >disttest/config.site 137 | cd disttest && CONFIG_SITE=config.site ../$(distdir)/configure 138 | $(MAKE) -C disttest 139 | $(MAKE) -C disttest check 140 | $(MAKE) -C disttest dist 141 | 142 | .PHONY: tags 143 | tags: 144 | rm -f tags 145 | ctags lib/*.c lib/*.h 146 | 147 | TRACKFS = trackfs 148 | trackinstall: 149 | $(TRACKFS) -l install.log -b backup.cpio $(MAKE) install 150 | 151 | # For the justification of the following Makefile rules, see node 152 | # `Automatic Remaking' in GNU Autoconf documentation. 153 | 154 | $(MAINT)$(srcdir)/configure: $(srcdir)/configure.in $(srcdir)/aclocal.m4 155 | $(RM) $(srcdir)/configure 156 | cd $(srcdir) && autoconf 157 | 158 | $(MAINT)$(srcdir)/config.h.in: $(srcdir)/stamp-h.in 159 | $(MAINT)$(srcdir)/stamp-h.in: $(srcdir)/configure.in $(srcdir)/acconfig.h 160 | $(RM) $(srcdir)/config.h.in 161 | cd $(srcdir) && autoheader 162 | cd $(srcdir) && $(RM) stamp-h.in && echo timestamp > stamp-h.in 163 | 164 | $(MAINT)config.h: stamp-h 165 | $(MAINT)stamp-h: config.h.in config.status 166 | CONFIG_FILES= CONFIG_HEADERS=config.h ./config.status 167 | $(RM) stamp-h && echo timestamp > stamp-h 168 | 169 | $(MAINT)Makefile: Makefile.in config.status 170 | CONFIG_FILES=$@ CONFIG_HEADERS= ./config.status 171 | 172 | $(MAINT)lib/Makefile: lib/Makefile.in config.status 173 | CONFIG_FILES=$@ CONFIG_HEADERS= ./config.status 174 | 175 | $(MAINT)lib/sys_elf.h: lib/stamp-h 176 | $(MAINT)lib/stamp-h: lib/sys_elf.h.in config.status 177 | CONFIG_FILES= CONFIG_HEADERS=lib/sys_elf.h ./config.status 178 | $(RM) lib/stamp-h && echo timestamp > lib/stamp-h 179 | 180 | $(MAINT)po/Makefile: po/Makefile.in config.status 181 | CONFIG_FILES=$@ CONFIG_HEADERS= ./config.status 182 | 183 | $(MAINT)libelf.pc: libelf.pc.in config.status 184 | CONFIG_FILES=$@ CONFIG_HEADERS= ./config.status 185 | 186 | RECHECK_FLAGS = CC='$(CC)' CPPFLAGS='$(CPPFLAGS)' \ 187 | CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS)' LIBS='$(LIBS)' 188 | 189 | $(MAINT)config.status: configure config.h.in VERSION 190 | $(RECHECK_FLAGS) ./config.status --recheck 191 | 192 | $(MAINT)reconfig: 193 | $(RM) config.cache 194 | $(RECHECK_FLAGS) ./config.status --recheck 195 | 196 | # Tell versions [3.59,3.63) of GNU make not to export all variables. 197 | # Otherwise a system limit (for SysV at least) may be exceeded. 198 | .NOEXPORT: 199 | -------------------------------------------------------------------------------- /Unix/.gitignore: -------------------------------------------------------------------------------- 1 | !Makefile 2 | libelf.so* 3 | 4 | -------------------------------------------------------------------------------- /Unix/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(origin SOLUTION_DIR), undefined) 2 | SOLUTION_DIR := $(shell pwd)/ 3 | endif 4 | PROJECT_DIR_LIBELF:=$(dir $(lastword $(MAKEFILE_LIST))) 5 | 6 | $(PROJECT_DIR_LIBELF)/../config.h: 7 | cd $(PROJECT_DIR_LIBELF)/.. && ./configure 8 | 9 | clean:: 10 | rm -f $(PROJECT_DIR_LIBELF)/../config.h 11 | rm -f libelf.so* 12 | rm -f libelf 13 | 14 | libelf : $(PROJECT_DIR_LIBELF)/../config.h 15 | cd $(PROJECT_DIR_LIBELF)/.. && make 16 | cp $(PROJECT_DIR_LIBELF)/../lib/libelf.so* . 17 | cd $(PROJECT_DIR_LIBELF)/.. && make -i clean 18 | touch libelf 19 | 20 | 21 | -------------------------------------------------------------------------------- /VC/.gitignore: -------------------------------------------------------------------------------- 1 | Release 2 | Debug 3 | check.c 4 | libelf.vcproj.* 5 | *.suo 6 | *.ncb -------------------------------------------------------------------------------- /VC/check.c.w32: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | -------------------------------------------------------------------------------- /VC/libelf.def: -------------------------------------------------------------------------------- 1 | LIBRARY 2 | VERSION 0.8 3 | EXPORTS 4 | elf_begin 5 | elf_cntl 6 | elf_delscn 7 | elf_end 8 | elf_errmsg 9 | elf_errno 10 | elf_fill 11 | elf_flagdata 12 | elf_flagehdr 13 | elf_flagelf 14 | elf_flagphdr 15 | elf_flagscn 16 | elf_flagshdr 17 | elf_getarhdr 18 | elf_getarsym 19 | elf_getbase 20 | elf_getdata 21 | elf_getident 22 | elf_getscn 23 | elf_hash 24 | elf_kind 25 | elf_memory 26 | elf_ndxscn 27 | elf_newdata 28 | elf_newscn 29 | elf_next 30 | elf_nextscn 31 | elf_rand 32 | elf_rawdata 33 | elf_rawfile 34 | elf_strptr 35 | elf_update 36 | elf_version 37 | elf32_checksum 38 | elf32_fsize 39 | elf32_getehdr 40 | elf32_getphdr 41 | elf32_getshdr 42 | elf32_newehdr 43 | elf32_newphdr 44 | elf32_xlatetof 45 | elf32_xlatetom 46 | elf64_checksum 47 | elf64_fsize 48 | elf64_getehdr 49 | elf64_getphdr 50 | elf64_getshdr 51 | elf64_newehdr 52 | elf64_newphdr 53 | elf64_xlatetof 54 | elf64_xlatetom 55 | elfx_movscn 56 | elfx_remscn 57 | gelf_checksum 58 | gelf_fsize 59 | gelf_getclass 60 | gelf_getdyn 61 | gelf_getehdr 62 | gelf_getphdr 63 | gelf_getrel 64 | gelf_getrela 65 | gelf_getshdr 66 | gelf_getsym 67 | gelf_msize 68 | gelf_newehdr 69 | gelf_newphdr 70 | gelf_update_dyn 71 | gelf_update_ehdr 72 | gelf_update_phdr 73 | gelf_update_rel 74 | gelf_update_rela 75 | gelf_update_shdr 76 | gelf_update_sym 77 | gelf_xlatetof 78 | gelf_xlatetom 79 | elf_getphnum 80 | elf_getshnum 81 | elf_getshstrndx 82 | elfx_update_shstrndx 83 | -------------------------------------------------------------------------------- /VC/libelf.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual Studio 2008 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libelf", "libelf.vcproj", "{1F6C9A28-676D-4257-8E17-0F22C5E82F4D}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Release|Win32 = Release|Win32 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {1F6C9A28-676D-4257-8E17-0F22C5E82F4D}.Debug|Win32.ActiveCfg = Debug|Win32 13 | {1F6C9A28-676D-4257-8E17-0F22C5E82F4D}.Debug|Win32.Build.0 = Debug|Win32 14 | {1F6C9A28-676D-4257-8E17-0F22C5E82F4D}.Release|Win32.ActiveCfg = Release|Win32 15 | {1F6C9A28-676D-4257-8E17-0F22C5E82F4D}.Release|Win32.Build.0 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /VC/prebuild.bat: -------------------------------------------------------------------------------- 1 | echo running prebuild.bat 2 | copy ..\lib\config.h.w32 ..\lib\config.h 3 | copy ..\lib\sys_elf.h.w32 ..\lib\sys_elf.h 4 | copy check.c.w32 check.c -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.8.12 2 | -------------------------------------------------------------------------------- /acconfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * acconfig.h - Special definitions for libelf, processed by autoheader. 3 | * Copyright (C) 1995 - 2001, 2004, 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | /* @(#) $Id: acconfig.h,v 1.16 2008/05/23 08:17:56 michael Exp $ */ 21 | 22 | /* Define if you want to include extra debugging code */ 23 | #undef ENABLE_DEBUG 24 | 25 | /* Define if you want to support extended ELF formats */ 26 | #undef ENABLE_EXTENDED_FORMAT 27 | 28 | /* Define if you want ELF format sanity checks by default */ 29 | #undef ENABLE_SANITY_CHECKS 30 | 31 | /* Define if memmove() does not copy overlapping arrays correctly */ 32 | #undef HAVE_BROKEN_MEMMOVE 33 | 34 | /* Define if you have the catgets function. */ 35 | #undef HAVE_CATGETS 36 | 37 | /* Define if you have the dgettext function. */ 38 | #undef HAVE_DGETTEXT 39 | 40 | /* Define if you have the memset function. */ 41 | #undef HAVE_MEMSET 42 | 43 | /* Define if struct nlist is declared in or */ 44 | #undef HAVE_STRUCT_NLIST_DECLARATION 45 | 46 | /* Define if Elf32_Dyn is declared in */ 47 | #undef __LIBELF_NEED_LINK_H 48 | 49 | /* Define if Elf32_Dyn is declared in */ 50 | #undef __LIBELF_NEED_SYS_LINK_H 51 | 52 | /* Define to `' or `' if one of them is present */ 53 | #undef __LIBELF_HEADER_ELF_H 54 | 55 | /* Define if you want 64-bit support (and your system supports it) */ 56 | #undef __LIBELF64 57 | 58 | /* Define if you want 64-bit support, and are running IRIX */ 59 | #undef __LIBELF64_IRIX 60 | 61 | /* Define if you want 64-bit support, and are running Linux */ 62 | #undef __LIBELF64_LINUX 63 | 64 | /* Define if you want symbol versioning (and your system supports it) */ 65 | #undef __LIBELF_SYMBOL_VERSIONS 66 | 67 | /* Define if symbol versioning uses Sun section type (SHT_SUNW_*) */ 68 | #undef __LIBELF_SUN_SYMBOL_VERSIONS 69 | 70 | /* Define if symbol versioning uses GNU section types (SHT_GNU_*) */ 71 | #undef __LIBELF_GNU_SYMBOL_VERSIONS 72 | 73 | /* Define to a 64-bit signed integer type if one exists */ 74 | #undef __libelf_i64_t 75 | 76 | /* Define to a 64-bit unsigned integer type if one exists */ 77 | #undef __libelf_u64_t 78 | 79 | /* Define to a 32-bit signed integer type if one exists */ 80 | #undef __libelf_i32_t 81 | 82 | /* Define to a 32-bit unsigned integer type if one exists */ 83 | #undef __libelf_u32_t 84 | 85 | /* Define to a 16-bit signed integer type if one exists */ 86 | #undef __libelf_i16_t 87 | 88 | /* Define to a 16-bit unsigned integer type if one exists */ 89 | #undef __libelf_u16_t 90 | -------------------------------------------------------------------------------- /config.h.in: -------------------------------------------------------------------------------- 1 | /* config.h.in. Generated automatically from configure.in by autoheader. */ 2 | 3 | /* Define to empty if the keyword does not work. */ 4 | #undef const 5 | 6 | /* Define if you have a working `mmap' system call. */ 7 | #undef HAVE_MMAP 8 | 9 | /* Define to `long' if doesn't define. */ 10 | #undef off_t 11 | 12 | /* Define to `unsigned' if doesn't define. */ 13 | #undef size_t 14 | 15 | /* Define if you have the ANSI C header files. */ 16 | #undef STDC_HEADERS 17 | 18 | /* Define if you want to include extra debugging code */ 19 | #undef ENABLE_DEBUG 20 | 21 | /* Define if you want to support extended ELF formats */ 22 | #undef ENABLE_EXTENDED_FORMAT 23 | 24 | /* Define if you want ELF format sanity checks by default */ 25 | #undef ENABLE_SANITY_CHECKS 26 | 27 | /* Define if memmove() does not copy overlapping arrays correctly */ 28 | #undef HAVE_BROKEN_MEMMOVE 29 | 30 | /* Define if you have the catgets function. */ 31 | #undef HAVE_CATGETS 32 | 33 | /* Define if you have the dgettext function. */ 34 | #undef HAVE_DGETTEXT 35 | 36 | /* Define if you have the memset function. */ 37 | #undef HAVE_MEMSET 38 | 39 | /* Define if struct nlist is declared in or */ 40 | #undef HAVE_STRUCT_NLIST_DECLARATION 41 | 42 | /* Define if Elf32_Dyn is declared in */ 43 | #undef __LIBELF_NEED_LINK_H 44 | 45 | /* Define if Elf32_Dyn is declared in */ 46 | #undef __LIBELF_NEED_SYS_LINK_H 47 | 48 | /* Define to `' or `' if one of them is present */ 49 | #undef __LIBELF_HEADER_ELF_H 50 | 51 | /* Define if you want 64-bit support (and your system supports it) */ 52 | #undef __LIBELF64 53 | 54 | /* Define if you want 64-bit support, and are running IRIX */ 55 | #undef __LIBELF64_IRIX 56 | 57 | /* Define if you want 64-bit support, and are running Linux */ 58 | #undef __LIBELF64_LINUX 59 | 60 | /* Define if you want symbol versioning (and your system supports it) */ 61 | #undef __LIBELF_SYMBOL_VERSIONS 62 | 63 | /* Define if symbol versioning uses Sun section type (SHT_SUNW_*) */ 64 | #undef __LIBELF_SUN_SYMBOL_VERSIONS 65 | 66 | /* Define if symbol versioning uses GNU section types (SHT_GNU_*) */ 67 | #undef __LIBELF_GNU_SYMBOL_VERSIONS 68 | 69 | /* Define to a 64-bit signed integer type if one exists */ 70 | #undef __libelf_i64_t 71 | 72 | /* Define to a 64-bit unsigned integer type if one exists */ 73 | #undef __libelf_u64_t 74 | 75 | /* Define to a 32-bit signed integer type if one exists */ 76 | #undef __libelf_i32_t 77 | 78 | /* Define to a 32-bit unsigned integer type if one exists */ 79 | #undef __libelf_u32_t 80 | 81 | /* Define to a 16-bit signed integer type if one exists */ 82 | #undef __libelf_i16_t 83 | 84 | /* Define to a 16-bit unsigned integer type if one exists */ 85 | #undef __libelf_u16_t 86 | 87 | /* The number of bytes in a __int64. */ 88 | #undef SIZEOF___INT64 89 | 90 | /* The number of bytes in a int. */ 91 | #undef SIZEOF_INT 92 | 93 | /* The number of bytes in a long. */ 94 | #undef SIZEOF_LONG 95 | 96 | /* The number of bytes in a long long. */ 97 | #undef SIZEOF_LONG_LONG 98 | 99 | /* The number of bytes in a short. */ 100 | #undef SIZEOF_SHORT 101 | 102 | /* Define if you have the ftruncate function. */ 103 | #undef HAVE_FTRUNCATE 104 | 105 | /* Define if you have the getpagesize function. */ 106 | #undef HAVE_GETPAGESIZE 107 | 108 | /* Define if you have the memcmp function. */ 109 | #undef HAVE_MEMCMP 110 | 111 | /* Define if you have the memcpy function. */ 112 | #undef HAVE_MEMCPY 113 | 114 | /* Define if you have the memmove function. */ 115 | #undef HAVE_MEMMOVE 116 | 117 | /* Define if you have the memset function. */ 118 | #undef HAVE_MEMSET 119 | 120 | /* Define if you have the header file. */ 121 | #undef HAVE_AR_H 122 | 123 | /* Define if you have the header file. */ 124 | #undef HAVE_ELF_H 125 | 126 | /* Define if you have the header file. */ 127 | #undef HAVE_FCNTL_H 128 | 129 | /* Define if you have the header file. */ 130 | #undef HAVE_GELF_H 131 | 132 | /* Define if you have the header file. */ 133 | #undef HAVE_LIBELF_H 134 | 135 | /* Define if you have the header file. */ 136 | #undef HAVE_LINK_H 137 | 138 | /* Define if you have the header file. */ 139 | #undef HAVE_NLIST_H 140 | 141 | /* Define if you have the header file. */ 142 | #undef HAVE_STDINT_H 143 | 144 | /* Define if you have the header file. */ 145 | #undef HAVE_SYS_ELF_H 146 | 147 | /* Define if you have the header file. */ 148 | #undef HAVE_SYS_LINK_H 149 | 150 | /* Define if you have the header file. */ 151 | #undef HAVE_UNISTD_H 152 | -------------------------------------------------------------------------------- /install-sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # install - install a program, script, or datafile 5 | # This comes from X11R5; it is not part of GNU. 6 | # 7 | # $XConsortium: install.sh,v 1.2 89/12/18 14:47:22 jim Exp $ 8 | # 9 | # This script is compatible with the BSD install script, but was written 10 | # from scratch. 11 | # 12 | 13 | 14 | # set DOITPROG to echo to test this script 15 | 16 | # Don't use :- since 4.3BSD and earlier shells don't like it. 17 | doit="${DOITPROG-}" 18 | 19 | 20 | # put in absolute paths if you don't have them in your path; or use env. vars. 21 | 22 | mvprog="${MVPROG-mv}" 23 | cpprog="${CPPROG-cp}" 24 | chmodprog="${CHMODPROG-chmod}" 25 | chownprog="${CHOWNPROG-chown}" 26 | chgrpprog="${CHGRPPROG-chgrp}" 27 | stripprog="${STRIPPROG-strip}" 28 | rmprog="${RMPROG-rm}" 29 | 30 | instcmd="$mvprog" 31 | chmodcmd="" 32 | chowncmd="" 33 | chgrpcmd="" 34 | stripcmd="" 35 | rmcmd="$rmprog -f" 36 | mvcmd="$mvprog" 37 | src="" 38 | dst="" 39 | 40 | while [ x"$1" != x ]; do 41 | case $1 in 42 | -c) instcmd="$cpprog" 43 | shift 44 | continue;; 45 | 46 | -m) chmodcmd="$chmodprog $2" 47 | shift 48 | shift 49 | continue;; 50 | 51 | -o) chowncmd="$chownprog $2" 52 | shift 53 | shift 54 | continue;; 55 | 56 | -g) chgrpcmd="$chgrpprog $2" 57 | shift 58 | shift 59 | continue;; 60 | 61 | -s) stripcmd="$stripprog" 62 | shift 63 | continue;; 64 | 65 | *) if [ x"$src" = x ] 66 | then 67 | src=$1 68 | else 69 | dst=$1 70 | fi 71 | shift 72 | continue;; 73 | esac 74 | done 75 | 76 | if [ x"$src" = x ] 77 | then 78 | echo "install: no input file specified" 79 | exit 1 80 | fi 81 | 82 | if [ x"$dst" = x ] 83 | then 84 | echo "install: no destination specified" 85 | exit 1 86 | fi 87 | 88 | 89 | # If destination is a directory, append the input filename; if your system 90 | # does not like double slashes in filenames, you may need to add some logic 91 | 92 | if [ -d $dst ] 93 | then 94 | dst="$dst"/`basename $src` 95 | fi 96 | 97 | # Make a temp file name in the proper directory. 98 | 99 | dstdir=`dirname $dst` 100 | dsttmp=$dstdir/#inst.$$# 101 | 102 | # Move or copy the file name to the temp name 103 | 104 | $doit $instcmd $src $dsttmp 105 | 106 | # and set any options; do chmod last to preserve setuid bits 107 | 108 | if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; fi 109 | if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; fi 110 | if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; fi 111 | if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; fi 112 | 113 | # Now rename the file to the real destination. 114 | 115 | $doit $rmcmd $dst 116 | $doit $mvcmd $dsttmp $dst 117 | 118 | 119 | exit 0 120 | -------------------------------------------------------------------------------- /lib/.gitignore: -------------------------------------------------------------------------------- 1 | config.h 2 | sys_elf.h -------------------------------------------------------------------------------- /lib/32.fsize.c: -------------------------------------------------------------------------------- 1 | /* 2 | 32.fsize.c - implementation of the elf{32,64}_fsize(3) functions. 3 | Copyright (C) 1995 - 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #ifndef lint 24 | static const char rcsid[] = "@(#) $Id: 32.fsize.c,v 1.13 2008/05/23 08:15:33 michael Exp $"; 25 | #endif /* lint */ 26 | 27 | const size_t 28 | _elf_fmsize[2][EV_CURRENT - EV_NONE][ELF_T_NUM][2] = { 29 | /* ELFCLASS32 */ 30 | { 31 | /* version 1 */ 32 | { 33 | { sizeof(unsigned char), sizeof(unsigned char) }, 34 | { sizeof(Elf32_Addr), sizeof(__ext_Elf32_Addr) }, 35 | { sizeof(Elf32_Dyn), sizeof(__ext_Elf32_Dyn) }, 36 | { sizeof(Elf32_Ehdr), sizeof(__ext_Elf32_Ehdr) }, 37 | { sizeof(Elf32_Half), sizeof(__ext_Elf32_Half) }, 38 | { sizeof(Elf32_Off), sizeof(__ext_Elf32_Off) }, 39 | { sizeof(Elf32_Phdr), sizeof(__ext_Elf32_Phdr) }, 40 | { sizeof(Elf32_Rela), sizeof(__ext_Elf32_Rela) }, 41 | { sizeof(Elf32_Rel), sizeof(__ext_Elf32_Rel) }, 42 | { sizeof(Elf32_Shdr), sizeof(__ext_Elf32_Shdr) }, 43 | { sizeof(Elf32_Sword), sizeof(__ext_Elf32_Sword) }, 44 | { sizeof(Elf32_Sym), sizeof(__ext_Elf32_Sym) }, 45 | { sizeof(Elf32_Word), sizeof(__ext_Elf32_Word) }, 46 | { 0, 0 }, /* there is no Elf32_Sxword */ 47 | { 0, 0 }, /* there is no Elf32_Xword */ 48 | /* XXX: check Solaris values */ 49 | { 0, 0 }, /* Elf32_Verdef/Verdaux size varies */ 50 | { 0, 0 }, /* Elf32_Verneed/Vernaux size varies */ 51 | }, 52 | }, 53 | #if __LIBELF64 54 | /* ELFCLASS64 */ 55 | { 56 | /* version 1 */ 57 | { 58 | { sizeof(unsigned char), sizeof(unsigned char) }, 59 | { sizeof(Elf64_Addr), sizeof(__ext_Elf64_Addr) }, 60 | { sizeof(Elf64_Dyn), sizeof(__ext_Elf64_Dyn) }, 61 | { sizeof(Elf64_Ehdr), sizeof(__ext_Elf64_Ehdr) }, 62 | { sizeof(Elf64_Half), sizeof(__ext_Elf64_Half) }, 63 | { sizeof(Elf64_Off), sizeof(__ext_Elf64_Off) }, 64 | { sizeof(Elf64_Phdr), sizeof(__ext_Elf64_Phdr) }, 65 | { sizeof(Elf64_Rela), sizeof(__ext_Elf64_Rela) }, 66 | { sizeof(Elf64_Rel), sizeof(__ext_Elf64_Rel) }, 67 | { sizeof(Elf64_Shdr), sizeof(__ext_Elf64_Shdr) }, 68 | { sizeof(Elf64_Sword), sizeof(__ext_Elf64_Sword) }, 69 | { sizeof(Elf64_Sym), sizeof(__ext_Elf64_Sym) }, 70 | { sizeof(Elf64_Word), sizeof(__ext_Elf64_Word) }, 71 | { sizeof(Elf64_Sxword), sizeof(__ext_Elf64_Sxword) }, 72 | { sizeof(Elf64_Xword), sizeof(__ext_Elf64_Xword) }, 73 | /* XXX: check Solaris values */ 74 | { 0, 0 }, /* Elf64_Verdef/Verdaux size varies */ 75 | { 0, 0 }, /* Elf64_Verneed/Vernaux size varies */ 76 | }, 77 | }, 78 | #endif /* __LIBELF64 */ 79 | }; 80 | 81 | static size_t 82 | _elf_fsize(unsigned cls, Elf_Type type, unsigned ver) { 83 | size_t n = 0; 84 | 85 | if (!valid_version(ver)) { 86 | seterr(ERROR_UNKNOWN_VERSION); 87 | } 88 | else if (!valid_type(type)) { 89 | seterr(ERROR_UNKNOWN_TYPE); 90 | } 91 | else if (!(n = _fsize(cls, ver, type))) { 92 | seterr(ERROR_UNKNOWN_TYPE); 93 | } 94 | return n; 95 | } 96 | 97 | size_t 98 | elf32_fsize(Elf_Type type, size_t count, unsigned ver) { 99 | return count * _elf_fsize(ELFCLASS32, type, ver); 100 | } 101 | 102 | #if __LIBELF64 103 | 104 | size_t 105 | elf64_fsize(Elf_Type type, size_t count, unsigned ver) { 106 | return count * _elf_fsize(ELFCLASS64, type, ver); 107 | } 108 | 109 | size_t 110 | gelf_fsize(Elf *elf, Elf_Type type, size_t count, unsigned ver) { 111 | if (elf) { 112 | if (elf->e_kind != ELF_K_ELF) { 113 | seterr(ERROR_NOTELF); 114 | } 115 | else if (valid_class(elf->e_class)) { 116 | return count * _elf_fsize(elf->e_class, type, ver); 117 | } 118 | else { 119 | seterr(ERROR_UNKNOWN_CLASS); 120 | } 121 | } 122 | return 0; 123 | } 124 | 125 | /* 126 | * Extension: report memory size 127 | */ 128 | size_t 129 | gelf_msize(Elf *elf, Elf_Type type, size_t count, unsigned ver) { 130 | size_t n; 131 | 132 | if (elf) { 133 | if (elf->e_kind != ELF_K_ELF) { 134 | seterr(ERROR_NOTELF); 135 | } 136 | else if (!valid_class(elf->e_class)) { 137 | seterr(ERROR_UNKNOWN_CLASS); 138 | } 139 | else if (!valid_version(ver)) { 140 | seterr(ERROR_UNKNOWN_VERSION); 141 | } 142 | else if (!valid_type(type)) { 143 | seterr(ERROR_UNKNOWN_TYPE); 144 | } 145 | else if (!(n = _msize(elf->e_class, ver, type))) { 146 | seterr(ERROR_UNKNOWN_TYPE); 147 | } 148 | else { 149 | return count * n; 150 | } 151 | } 152 | return 0; 153 | } 154 | 155 | #endif /* __LIBELF64 */ 156 | -------------------------------------------------------------------------------- /lib/32.getehdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | 32.getehdr.c - implementation of the elf{32,64}_getehdr(3) functions. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: 32.getehdr.c,v 1.9 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | char* 27 | _elf_getehdr(Elf *elf, unsigned cls) { 28 | if (!elf) { 29 | return NULL; 30 | } 31 | elf_assert(elf->e_magic == ELF_MAGIC); 32 | if (elf->e_kind != ELF_K_ELF) { 33 | seterr(ERROR_NOTELF); 34 | } 35 | else if (elf->e_class != cls) { 36 | seterr(ERROR_CLASSMISMATCH); 37 | } 38 | else if (elf->e_ehdr || _elf_cook(elf)) { 39 | return elf->e_ehdr; 40 | } 41 | return NULL; 42 | } 43 | 44 | Elf32_Ehdr* 45 | elf32_getehdr(Elf *elf) { 46 | return (Elf32_Ehdr*)_elf_getehdr(elf, ELFCLASS32); 47 | } 48 | 49 | #if __LIBELF64 50 | 51 | Elf64_Ehdr* 52 | elf64_getehdr(Elf *elf) { 53 | return (Elf64_Ehdr*)_elf_getehdr(elf, ELFCLASS64); 54 | } 55 | 56 | #endif /* __LIBELF64 */ 57 | -------------------------------------------------------------------------------- /lib/32.getphdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | 32.getphdr.c - implementation of the elf{32,64}_getphdr(3) functions. 3 | Copyright (C) 1995 - 2000 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: 32.getphdr.c,v 1.11 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | char* 27 | _elf_getphdr(Elf *elf, unsigned cls) { 28 | if (!elf) { 29 | return NULL; 30 | } 31 | elf_assert(elf->e_magic == ELF_MAGIC); 32 | if (elf->e_kind != ELF_K_ELF) { 33 | seterr(ERROR_NOTELF); 34 | } 35 | else if (elf->e_class != cls) { 36 | seterr(ERROR_CLASSMISMATCH); 37 | } 38 | else if (elf->e_ehdr || _elf_cook(elf)) { 39 | return elf->e_phdr; 40 | } 41 | return NULL; 42 | } 43 | 44 | Elf32_Phdr* 45 | elf32_getphdr(Elf *elf) { 46 | return (Elf32_Phdr*)_elf_getphdr(elf, ELFCLASS32); 47 | } 48 | 49 | #if __LIBELF64 50 | 51 | Elf64_Phdr* 52 | elf64_getphdr(Elf *elf) { 53 | return (Elf64_Phdr*)_elf_getphdr(elf, ELFCLASS64); 54 | } 55 | 56 | #endif /* __LIBELF64 */ 57 | -------------------------------------------------------------------------------- /lib/32.getshdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | 32.getshdr.c - implementation of the elf{32,64}_getshdr(3) functions. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: 32.getshdr.c,v 1.10 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | Elf32_Shdr* 27 | elf32_getshdr(Elf_Scn *scn) { 28 | if (!scn) { 29 | return NULL; 30 | } 31 | elf_assert(scn->s_magic == SCN_MAGIC); 32 | elf_assert(scn->s_elf); 33 | elf_assert(scn->s_elf->e_magic == ELF_MAGIC); 34 | if (scn->s_elf->e_class == ELFCLASS32) { 35 | return &scn->s_shdr32; 36 | } 37 | seterr(ERROR_CLASSMISMATCH); 38 | return NULL; 39 | } 40 | 41 | #if __LIBELF64 42 | 43 | Elf64_Shdr* 44 | elf64_getshdr(Elf_Scn *scn) { 45 | if (!scn) { 46 | return NULL; 47 | } 48 | elf_assert(scn->s_magic == SCN_MAGIC); 49 | elf_assert(scn->s_elf); 50 | elf_assert(scn->s_elf->e_magic == ELF_MAGIC); 51 | if (scn->s_elf->e_class == ELFCLASS64) { 52 | return &scn->s_shdr64; 53 | } 54 | seterr(ERROR_CLASSMISMATCH); 55 | return NULL; 56 | } 57 | 58 | #endif /* __LIBELF64 */ 59 | -------------------------------------------------------------------------------- /lib/32.newehdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 32.newehdr.c - implementation of the elf{32,64}_newehdr(3) functions. 3 | * Copyright (C) 1995 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: 32.newehdr.c,v 1.16 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | static char* 27 | _elf_newehdr(Elf *elf, unsigned cls) { 28 | size_t size; 29 | 30 | if (!elf) { 31 | return NULL; 32 | } 33 | elf_assert(elf->e_magic == ELF_MAGIC); 34 | if (elf->e_readable) { 35 | return _elf_getehdr(elf, cls); 36 | } 37 | else if (!elf->e_ehdr) { 38 | size = _msize(cls, _elf_version, ELF_T_EHDR); 39 | elf_assert(size); 40 | if ((elf->e_ehdr = (char*)malloc(size))) { 41 | memset(elf->e_ehdr, 0, size); 42 | elf->e_ehdr_flags |= ELF_F_DIRTY; 43 | elf->e_kind = ELF_K_ELF; 44 | elf->e_class = cls; 45 | return elf->e_ehdr; 46 | } 47 | seterr(ERROR_MEM_EHDR); 48 | } 49 | else if (elf->e_class != cls) { 50 | seterr(ERROR_CLASSMISMATCH); 51 | } 52 | else { 53 | elf_assert(elf->e_kind == ELF_K_ELF); 54 | return elf->e_ehdr; 55 | } 56 | return NULL; 57 | } 58 | 59 | Elf32_Ehdr* 60 | elf32_newehdr(Elf *elf) { 61 | return (Elf32_Ehdr*)_elf_newehdr(elf, ELFCLASS32); 62 | } 63 | 64 | #if __LIBELF64 65 | 66 | Elf64_Ehdr* 67 | elf64_newehdr(Elf *elf) { 68 | return (Elf64_Ehdr*)_elf_newehdr(elf, ELFCLASS64); 69 | } 70 | 71 | unsigned long 72 | gelf_newehdr(Elf *elf, int cls) { 73 | if (!valid_class(cls) || !_msize(cls, _elf_version, ELF_T_EHDR)) { 74 | seterr(ERROR_UNKNOWN_CLASS); 75 | return 0; 76 | } 77 | return (unsigned long)_elf_newehdr(elf, cls); 78 | } 79 | 80 | #endif /* __LIBELF64 */ 81 | -------------------------------------------------------------------------------- /lib/32.newphdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 32.newphdr.c - implementation of the elf{32,64}_newphdr(3) functions. 3 | * Copyright (C) 1995 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: 32.newphdr.c,v 1.16 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | static char* 27 | _elf_newphdr(Elf *elf, size_t count, unsigned cls) { 28 | size_t extcount = 0; 29 | Elf_Scn *scn = NULL; 30 | char *phdr = NULL; 31 | size_t size; 32 | 33 | if (!elf) { 34 | return NULL; 35 | } 36 | elf_assert(elf->e_magic == ELF_MAGIC); 37 | if (!elf->e_ehdr && !elf->e_readable) { 38 | seterr(ERROR_NOEHDR); 39 | } 40 | else if (elf->e_kind != ELF_K_ELF) { 41 | seterr(ERROR_NOTELF); 42 | } 43 | else if (elf->e_class != cls) { 44 | seterr(ERROR_CLASSMISMATCH); 45 | } 46 | else if (elf->e_ehdr || _elf_cook(elf)) { 47 | size = _msize(cls, _elf_version, ELF_T_PHDR); 48 | elf_assert(size); 49 | if (!(scn = _elf_first_scn(elf))) { 50 | return NULL; 51 | } 52 | if (count) { 53 | if (!(phdr = (char*)malloc(count * size))) { 54 | seterr(ERROR_MEM_PHDR); 55 | return NULL; 56 | } 57 | memset(phdr, 0, count * size); 58 | } 59 | elf_assert(elf->e_ehdr); 60 | elf->e_phnum = count; 61 | if (count >= PN_XNUM) { 62 | /* 63 | * get NULL section (create it if necessary) 64 | */ 65 | extcount = count; 66 | count = PN_XNUM; 67 | } 68 | if (cls == ELFCLASS32) { 69 | ((Elf32_Ehdr*)elf->e_ehdr)->e_phnum = count; 70 | scn->s_shdr32.sh_info = extcount; 71 | } 72 | #if __LIBELF64 73 | else if (cls == ELFCLASS64) { 74 | ((Elf64_Ehdr*)elf->e_ehdr)->e_phnum = count; 75 | scn->s_shdr64.sh_info = extcount; 76 | } 77 | #endif /* __LIBELF64 */ 78 | else { 79 | seterr(ERROR_UNIMPLEMENTED); 80 | if (phdr) { 81 | free(phdr); 82 | } 83 | return NULL; 84 | } 85 | if (elf->e_phdr) { 86 | free(elf->e_phdr); 87 | } 88 | elf->e_phdr = phdr; 89 | elf->e_phdr_flags |= ELF_F_DIRTY; 90 | elf->e_ehdr_flags |= ELF_F_DIRTY; 91 | scn->s_scn_flags |= ELF_F_DIRTY; 92 | return phdr; 93 | } 94 | return NULL; 95 | } 96 | 97 | Elf32_Phdr* 98 | elf32_newphdr(Elf *elf, size_t count) { 99 | return (Elf32_Phdr*)_elf_newphdr(elf, count, ELFCLASS32); 100 | } 101 | 102 | #if __LIBELF64 103 | 104 | Elf64_Phdr* 105 | elf64_newphdr(Elf *elf, size_t count) { 106 | return (Elf64_Phdr*)_elf_newphdr(elf, count, ELFCLASS64); 107 | } 108 | 109 | unsigned long 110 | gelf_newphdr(Elf *elf, size_t phnum) { 111 | if (!valid_class(elf->e_class)) { 112 | seterr(ERROR_UNKNOWN_CLASS); 113 | return 0; 114 | } 115 | return (unsigned long)_elf_newphdr(elf, phnum, elf->e_class); 116 | } 117 | 118 | #endif /* __LIBELF64 */ 119 | -------------------------------------------------------------------------------- /lib/Makefile.w32: -------------------------------------------------------------------------------- 1 | # lib/Makefile.w32 - Makefile for W32 port. 2 | # Copyright (C) 1995 - 2006 Michael Riepe 3 | # 4 | # This library is free software; you can redistribute it and/or 5 | # modify it under the terms of the GNU Library General Public 6 | # License as published by the Free Software Foundation; either 7 | # version 2 of the License, or (at your option) any later version. 8 | # 9 | # This library is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | # Library General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU Library General Public 15 | # License along with this library; if not, write to the Free Software 16 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | 18 | # @(#) $Id: Makefile.w32,v 1.2 2006/11/21 20:21:12 michael Exp $ 19 | 20 | instroot = 21 | 22 | prefix = 23 | exec_prefix = 24 | libdir = 25 | includedir = 26 | installdirs = $(libdir) $(includedir) $(includedir)/libelf 27 | 28 | CC = cl /nologo 29 | LD = link /nologo 30 | AR = 31 | MV = 32 | RM = del 33 | LN_S = 34 | RANLIB = 35 | INSTALL = 36 | INSTALL_DATA = 37 | INSTALL_PROGRAM = 38 | 39 | CFLAGS = /O2 /W2 /TC /MD 40 | CPPFLAGS = 41 | DEFS = /DHAVE_CONFIG_H 42 | LDFLAGS = 43 | LIBS = 44 | DEPSHLIBS = 45 | 46 | DO_SHLIB = 47 | PICFLAGS = 48 | SHLIB_SFX = .dll 49 | SHLINK_SFX = 50 | SONAME_SFX = 51 | LINK_SHLIB = $(LD) /DLL $(LDFLAGS) 52 | 53 | SHLIB = libelf$(SHLIB_SFX) 54 | SHLINK = libelf$(SHLINK_SFX) 55 | SONAME = libelf$(SONAME_SFX) 56 | 57 | # install includes in includedir? 58 | DO_COMPAT = 59 | 60 | INCLUDES = /I. 61 | 62 | COMPILE = $(CC) /c $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $(XCFLAGS) 63 | 64 | # no user serviceable parts below 65 | 66 | PACKAGE = libelf 67 | VERSION = 0.8.9 68 | MAJOR = 0 69 | 70 | SHELL = /bin/sh 71 | 72 | srcdir = . 73 | top_srcdir = .. 74 | 75 | topdir = .. 76 | subdir = lib 77 | 78 | .SUFFIXES: 79 | .SUFFIXES: .obj .c 80 | .c.obj: 81 | $(COMPILE) $< 82 | 83 | # generic sources 84 | SRCS1 = begin.c cntl.c end.c errmsg.c errno.c fill.c flag.c getarhdr.c \ 85 | getarsym.c getbase.c getdata.c getident.c getscn.c hash.c kind.c \ 86 | ndxscn.c newdata.c newscn.c next.c nextscn.c rand.c rawdata.c \ 87 | rawfile.c strptr.c update.c version.c checksum.c 88 | OBJS1 = $(SRCS1:.c=.obj) 89 | 90 | # 32-bit sources 91 | SRCS2 = 32.fsize.c 32.getehdr.c 32.getphdr.c 32.getshdr.c 32.newehdr.c \ 92 | 32.newphdr.c 32.xlatetof.c 93 | OBJS2 = $(SRCS2:.c=.obj) 94 | 95 | # support 96 | SRCS3 = cook.c data.c input.c assert.c 97 | OBJS3 = $(SRCS3:.c=.obj) 98 | 99 | # nlist 100 | SRCS4 = nlist.c 101 | OBJS4 = $(SRCS4:.c=.obj) 102 | 103 | # opt 104 | SRCS5 = opt.delscn.c x.remscn.c x.movscn.c x.elfext.c 105 | OBJS5 = $(SRCS5:.c=.obj) 106 | 107 | # 64-bit sources 108 | SRCS64 = 64.xlatetof.c gelfehdr.c gelfphdr.c gelfshdr.c gelftrans.c swap64.c 109 | OBJS64 = $(SRCS64:.c=.obj) 110 | 111 | # Versioning sources 112 | SRCS_V = verdef_32_tof.c verdef_32_tom.c verdef_64_tof.c verdef_64_tom.c 113 | OBJS_V = $(SRCS_V:.c=.obj) 114 | HDRS_V = verdef.h verneed.h 115 | 116 | SRCS = $(SRCS1) $(SRCS2) $(SRCS3) $(SRCS4) $(SRCS5) $(SRCS64) $(SRCS_V) 117 | OBJS = $(OBJS1) $(OBJS2) $(OBJS3) $(OBJS4) $(OBJS5) $(OBJS64) $(OBJS_V) 118 | 119 | # missing functions 120 | LIBSRCS = memset.c 121 | LIBOBJS = 122 | 123 | # public header files 124 | HDRS = libelf.h nlist.h gelf.h 125 | 126 | # public header files (created by configure) 127 | AUXHDRS = sys_elf.h 128 | 129 | # private header files 130 | PRIVHDRS = byteswap.h errors.h ext_types.h private.h elf_repl.h $(HDRS_V) 131 | 132 | DISTFILES = $(SRCS) $(LIBSRCS) $(HDRS) $(PRIVHDRS) Makefile.in sys_elf.h.in 133 | 134 | all: $(OBJS) $(SHLIB) 135 | 136 | check: 137 | 138 | $(SHLIB): libelf.def $(OBJS) $(LIBOBJS) 139 | -@$(RM) $(SHLIB) 140 | $(LINK_SHLIB) /OUT:"$(SHLIB)" /DEF:"libelf.def" $(OBJS) $(LIBOBJS) kernel32.lib 141 | 142 | install: 143 | 144 | mostlyclean: 145 | -$(RM) *.obj 146 | -$(RM) $(SHLIB) 147 | -$(RM) libelf.lib 148 | -$(RM) libelf.exp 149 | 150 | clean: mostlyclean 151 | 152 | distclean: clean 153 | -$(RM) $(AUXHDRS) 154 | 155 | maintainer-clean: distclean 156 | 157 | # dependencies 158 | $(OBJS): private.h config.h libelf.h gelf.h errors.h $(AUXHDRS) 159 | 32.fsize.obj: ext_types.h 160 | 32.xlatetof.obj: byteswap.h ext_types.h 161 | 64.xlatetof.obj: byteswap.h ext_types.h 162 | getarsym.obj: byteswap.h 163 | memset.obj: config.h 164 | nlist.obj: nlist.h 165 | swap64.obj: byteswap.h 166 | $(OBJS_V): byteswap.h ext_types.h $(HDRS_V) 167 | -------------------------------------------------------------------------------- /lib/assert.c: -------------------------------------------------------------------------------- 1 | /* 2 | assert.c - assert function for libelf. 3 | Copyright (C) 1999 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: assert.c,v 1.5 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | #include 27 | 28 | void 29 | __elf_assert(const char *file, unsigned line, const char *cond) { 30 | fprintf(stderr, "%s:%u: libelf assertion failure: %s\n", 31 | file, line, cond); 32 | abort(); 33 | } 34 | -------------------------------------------------------------------------------- /lib/build.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | rem lib/build.bat - build script for W32 port 4 | rem Copyright (C) 2004 - 2006 Michael Riepe 5 | rem 6 | rem This library is free software; you can redistribute it and/or 7 | rem modify it under the terms of the GNU Library General Public 8 | rem License as published by the Free Software Foundation; either 9 | rem version 2 of the License, or (at your option) any later version. 10 | rem 11 | rem This library is distributed in the hope that it will be useful, 12 | rem but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | rem Library General Public License for more details. 15 | rem 16 | rem You should have received a copy of the GNU Library General Public 17 | rem License along with this library; if not, write to the Free Software 18 | rem Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 | 20 | rem @(#) $Id: build.bat,v 1.1 2006/08/21 18:03:48 michael Exp $ 21 | 22 | rem *** BEGIN EDIT HERE *** 23 | rem Please uncomment the line that suits your system: 24 | rem call "C:\Program Files\Microsoft Visual Studio\VC98\bin\vcvars32.bat" 25 | rem call "C:\Program Files\Microsoft Visual Studio 8\VC\bin\vcvars32.bat" 26 | rem call "C:\Programme\Microsoft Visual Studio\VC98\bin\vcvars32.bat" 27 | rem call "C:\Programme\Microsoft Visual Studio 8\VC\bin\vcvars32.bat" 28 | rem OR, if you have to set the path to the compiler directly: 29 | rem set PATH="C:\PATH\TO\COMPILER\BINARY;%PATH%" 30 | rem Of course, you'll have to enter the correct path above. 31 | rem You may also have to change CC (default: cl.exe) in Makefile.w32. 32 | rem *** END EDIT HERE *** 33 | 34 | copy config.h.w32 config.h 35 | copy sys_elf.h.w32 sys_elf.h 36 | nmake /nologo /f Makefile.w32 %1 37 | -------------------------------------------------------------------------------- /lib/byteswap.h: -------------------------------------------------------------------------------- 1 | /* 2 | byteswap.h - functions and macros for byte swapping. 3 | Copyright (C) 1995 - 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | /* @(#) $Id: byteswap.h,v 1.7 2008/05/23 08:15:34 michael Exp $ */ 21 | 22 | #ifndef _BYTESWAP_H 23 | #define _BYTESWAP_H 24 | 25 | #define lu(from,i,s) (((__libelf_u32_t)((unsigned char*)(from))[i])<<(s)) 26 | #define li(from,i,s) (((__libelf_i32_t)(( signed char*)(from))[i])<<(s)) 27 | 28 | #define __load_u16L(from) ((__libelf_u32_t) \ 29 | (lu(from,1,8) | lu(from,0,0))) 30 | #define __load_u16M(from) ((__libelf_u32_t) \ 31 | (lu(from,0,8) | lu(from,1,0))) 32 | #define __load_i16L(from) ((__libelf_i32_t) \ 33 | (li(from,1,8) | lu(from,0,0))) 34 | #define __load_i16M(from) ((__libelf_i32_t) \ 35 | (li(from,0,8) | lu(from,1,0))) 36 | 37 | #define __load_u32L(from) ((__libelf_u32_t) \ 38 | (lu(from,3,24) | lu(from,2,16) | lu(from,1,8) | lu(from,0,0))) 39 | #define __load_u32M(from) ((__libelf_u32_t) \ 40 | (lu(from,0,24) | lu(from,1,16) | lu(from,2,8) | lu(from,3,0))) 41 | #define __load_i32L(from) ((__libelf_i32_t) \ 42 | (li(from,3,24) | lu(from,2,16) | lu(from,1,8) | lu(from,0,0))) 43 | #define __load_i32M(from) ((__libelf_i32_t) \ 44 | (li(from,0,24) | lu(from,1,16) | lu(from,2,8) | lu(from,3,0))) 45 | 46 | #define su(to,i,v,s) (((char*)(to))[i]=((__libelf_u32_t)(v)>>(s))) 47 | #define si(to,i,v,s) (((char*)(to))[i]=((__libelf_i32_t)(v)>>(s))) 48 | 49 | #define __store_u16L(to,v) \ 50 | (su(to,1,v,8), su(to,0,v,0)) 51 | #define __store_u16M(to,v) \ 52 | (su(to,0,v,8), su(to,1,v,0)) 53 | #define __store_i16L(to,v) \ 54 | (si(to,1,v,8), si(to,0,v,0)) 55 | #define __store_i16M(to,v) \ 56 | (si(to,0,v,8), si(to,1,v,0)) 57 | 58 | #define __store_u32L(to,v) \ 59 | (su(to,3,v,24), su(to,2,v,16), su(to,1,v,8), su(to,0,v,0)) 60 | #define __store_u32M(to,v) \ 61 | (su(to,0,v,24), su(to,1,v,16), su(to,2,v,8), su(to,3,v,0)) 62 | #define __store_i32L(to,v) \ 63 | (si(to,3,v,24), si(to,2,v,16), si(to,1,v,8), si(to,0,v,0)) 64 | #define __store_i32M(to,v) \ 65 | (si(to,0,v,24), si(to,1,v,16), si(to,2,v,8), si(to,3,v,0)) 66 | 67 | #if __LIBELF64 68 | 69 | /* 70 | * conversion functions from swap64.c 71 | */ 72 | extern __libelf_u64_t _elf_load_u64L(const unsigned char *from); 73 | extern __libelf_u64_t _elf_load_u64M(const unsigned char *from); 74 | extern __libelf_i64_t _elf_load_i64L(const unsigned char *from); 75 | extern __libelf_i64_t _elf_load_i64M(const unsigned char *from); 76 | extern void _elf_store_u64L(unsigned char *to, __libelf_u64_t v); 77 | extern void _elf_store_u64M(unsigned char *to, __libelf_u64_t v); 78 | extern void _elf_store_i64L(unsigned char *to, __libelf_u64_t v); 79 | extern void _elf_store_i64M(unsigned char *to, __libelf_u64_t v); 80 | 81 | /* 82 | * aliases for existing conversion code 83 | */ 84 | #define __load_u64L _elf_load_u64L 85 | #define __load_u64M _elf_load_u64M 86 | #define __load_i64L _elf_load_i64L 87 | #define __load_i64M _elf_load_i64M 88 | #define __store_u64L _elf_store_u64L 89 | #define __store_u64M _elf_store_u64M 90 | #define __store_i64L _elf_store_i64L 91 | #define __store_i64M _elf_store_i64M 92 | 93 | #endif /* __LIBELF64 */ 94 | 95 | #endif /* _BYTESWAP_H */ 96 | -------------------------------------------------------------------------------- /lib/checksum.c: -------------------------------------------------------------------------------- 1 | /* 2 | checksum.c - implementation of the elf{32,64}_checksum(3) functions. 3 | Copyright (C) 1995 - 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: checksum.c,v 1.7 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | /* 27 | * Compatibility note: 28 | * 29 | * The algorithm used in {elf32,elf64,gelf}_checksum() does not seem to 30 | * be documented. I hope I got it right. My implementation does the 31 | * following: 32 | * 33 | * - skip sections that do not have the SHF_ALLOC flag set 34 | * - skip sections of type SHT_NULL, SHT_NOBITS, SHT_DYNSYM and 35 | * SHT_DYNAMIC 36 | * - add all data bytes from the remaining sections, modulo 2**32 37 | * - add upper and lower half of the result 38 | * - subtract 0xffff if the result is > 0xffff 39 | * - if any error occurs, return 0L 40 | */ 41 | 42 | static int 43 | skip_section(Elf_Scn *scn, unsigned cls) { 44 | if (cls == ELFCLASS32) { 45 | Elf32_Shdr *shdr = &scn->s_shdr32; 46 | 47 | if (!(shdr->sh_flags & SHF_ALLOC)) { 48 | return 1; 49 | } 50 | switch (shdr->sh_type) { 51 | case SHT_NULL: 52 | case SHT_NOBITS: 53 | /* Solaris seems to ignore these, too */ 54 | case SHT_DYNSYM: 55 | case SHT_DYNAMIC: 56 | return 1; 57 | } 58 | } 59 | #if __LIBELF64 60 | else if (cls == ELFCLASS64) { 61 | Elf64_Shdr *shdr = &scn->s_shdr64; 62 | 63 | if (!(shdr->sh_flags & SHF_ALLOC)) { 64 | return 1; 65 | } 66 | switch (shdr->sh_type) { 67 | case SHT_NULL: 68 | case SHT_NOBITS: 69 | /* Solaris seems to ignore these, too */ 70 | case SHT_DYNSYM: 71 | case SHT_DYNAMIC: 72 | return 1; 73 | } 74 | } 75 | #endif /* __LIBELF64 */ 76 | else { 77 | seterr(ERROR_UNIMPLEMENTED); 78 | } 79 | return 0; 80 | } 81 | 82 | static long 83 | add_bytes(unsigned char *ptr, size_t len) { 84 | long csum = 0; 85 | 86 | while (len--) { 87 | csum += *ptr++; 88 | } 89 | return csum; 90 | } 91 | 92 | static long 93 | _elf_csum(Elf *elf) { 94 | long csum = 0; 95 | Elf_Data *data; 96 | Elf_Scn *scn; 97 | 98 | if (!elf->e_ehdr && !_elf_cook(elf)) { 99 | /* propagate errors from _elf_cook */ 100 | return 0L; 101 | } 102 | seterr(0); 103 | for (scn = elf->e_scn_1; scn; scn = scn->s_link) { 104 | if (scn->s_index == SHN_UNDEF || skip_section(scn, elf->e_class)) { 105 | continue; 106 | } 107 | data = NULL; 108 | while ((data = elf_getdata(scn, data))) { 109 | if (data->d_size) { 110 | if (data->d_buf == NULL) { 111 | seterr(ERROR_NULLBUF); 112 | return 0L; 113 | } 114 | csum += add_bytes(data->d_buf, data->d_size); 115 | } 116 | } 117 | } 118 | if (_elf_errno) { 119 | return 0L; 120 | } 121 | csum = (csum & 0xffff) + ((csum >> 16) & 0xffff); 122 | if (csum > 0xffff) { 123 | csum -= 0xffff; 124 | } 125 | return csum; 126 | } 127 | 128 | long 129 | elf32_checksum(Elf *elf) { 130 | if (elf) { 131 | if (elf->e_kind != ELF_K_ELF) { 132 | seterr(ERROR_NOTELF); 133 | } 134 | else if (elf->e_class != ELFCLASS32) { 135 | seterr(ERROR_CLASSMISMATCH); 136 | } 137 | else { 138 | return _elf_csum(elf); 139 | } 140 | } 141 | return 0L; 142 | } 143 | 144 | #if __LIBELF64 145 | 146 | long 147 | elf64_checksum(Elf *elf) { 148 | if (elf) { 149 | if (elf->e_kind != ELF_K_ELF) { 150 | seterr(ERROR_NOTELF); 151 | } 152 | else if (elf->e_class != ELFCLASS64) { 153 | seterr(ERROR_CLASSMISMATCH); 154 | } 155 | else { 156 | return _elf_csum(elf); 157 | } 158 | } 159 | return 0L; 160 | } 161 | 162 | long 163 | gelf_checksum(Elf *elf) { 164 | if (elf) { 165 | if (elf->e_kind != ELF_K_ELF) { 166 | seterr(ERROR_NOTELF); 167 | } 168 | else if (!valid_class(elf->e_class)) { 169 | seterr(ERROR_UNKNOWN_CLASS); 170 | } 171 | else { 172 | return _elf_csum(elf); 173 | } 174 | } 175 | return 0L; 176 | } 177 | 178 | #endif /* __LIBELF64 */ 179 | -------------------------------------------------------------------------------- /lib/cntl.c: -------------------------------------------------------------------------------- 1 | /* 2 | cntl.c - implementation of the elf_cntl(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: cntl.c,v 1.7 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | int 27 | elf_cntl(Elf *elf, Elf_Cmd cmd) { 28 | Elf_Scn *scn; 29 | Elf *child; 30 | 31 | if (!elf) { 32 | return -1; 33 | } 34 | elf_assert(elf->e_magic == ELF_MAGIC); 35 | if (cmd == ELF_C_FDREAD) { 36 | if (!elf->e_readable) { 37 | seterr(ERROR_WRONLY); 38 | return -1; 39 | } 40 | } 41 | else if (cmd != ELF_C_FDDONE) { 42 | seterr(ERROR_INVALID_CMD); 43 | return -1; 44 | } 45 | if (elf->e_disabled) { 46 | return 0; 47 | } 48 | if (elf->e_kind == ELF_K_AR) { 49 | for (child = elf->e_members; child; child = child->e_link) { 50 | elf_assert(elf == child->e_parent); 51 | if (elf_cntl(child, cmd)) { 52 | return -1; 53 | } 54 | } 55 | } 56 | else if (elf->e_kind == ELF_K_ELF && cmd == ELF_C_FDREAD) { 57 | if (!elf->e_ehdr && !_elf_cook(elf)) { 58 | return -1; 59 | } 60 | for (scn = elf->e_scn_1; scn; scn = scn->s_link) { 61 | if (scn->s_index == SHN_UNDEF || scn->s_type == SHT_NULL) { 62 | continue; 63 | } 64 | else if (!elf_getdata(scn, NULL)) { 65 | return -1; 66 | } 67 | } 68 | } 69 | elf->e_disabled = 1; 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /lib/config.h.w32: -------------------------------------------------------------------------------- 1 | /* 2 | * lib/config.h.w32 - configuration file for W32 port 3 | * Copyright (C) 2004 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | * 19 | * @(#) $Id: config.h.w32,v 1.2 2006/09/07 15:55:42 michael Exp $ 20 | */ 21 | 22 | /* Define to empty if the keyword does not work. */ 23 | #undef const 24 | 25 | /* Define if you have a working `mmap' system call. */ 26 | #undef HAVE_MMAP 27 | 28 | /* Define to `long' if doesn't define. */ 29 | #undef off_t 30 | 31 | /* Define to `unsigned' if doesn't define. */ 32 | #undef size_t 33 | 34 | /* Define if you have the ANSI C header files. */ 35 | #define STDC_HEADERS 1 36 | 37 | /* Define if you want to include extra debugging code */ 38 | #define ENABLE_DEBUG 1 39 | 40 | /* Define if memmove() does not copy overlapping arrays correctly */ 41 | #undef HAVE_BROKEN_MEMMOVE 42 | 43 | /* Define if you have the catgets function. */ 44 | #undef HAVE_CATGETS 45 | 46 | /* Define if you have the dgettext function. */ 47 | #undef HAVE_DGETTEXT 48 | 49 | /* Define if you have the memset function. */ 50 | #define HAVE_MEMSET 1 51 | 52 | /* Define if struct nlist is declared in or */ 53 | #undef HAVE_STRUCT_NLIST_DECLARATION 54 | 55 | /* Define if Elf32_Dyn is declared in */ 56 | #undef __LIBELF_NEED_LINK_H 57 | 58 | /* Define if Elf32_Dyn is declared in */ 59 | #undef __LIBELF_NEED_SYS_LINK_H 60 | 61 | /* Define to `' or `' if one of them is present */ 62 | #undef __LIBELF_HEADER_ELF_H 63 | 64 | /* Define if you want 64-bit support (and your system supports it) */ 65 | #define __LIBELF64 1 66 | 67 | /* Define if you want 64-bit support, and are running IRIX */ 68 | #undef __LIBELF64_IRIX 69 | 70 | /* Define if you want 64-bit support, and are running Linux */ 71 | #undef __LIBELF64_LINUX 72 | 73 | /* Define if you want symbol versioning (and your system supports it) */ 74 | #define __LIBELF_SYMBOL_VERSIONS 1 75 | 76 | /* Define if symbol versioning uses Sun section type (SHT_SUNW_*) */ 77 | #define __LIBELF_SUN_SYMBOL_VERSIONS 1 78 | 79 | /* Define if symbol versioning uses GNU section types (SHT_GNU_*) */ 80 | #undef __LIBELF_GNU_SYMBOL_VERSIONS 81 | 82 | /* Define to a 64-bit signed integer type if one exists */ 83 | #define __libelf_i64_t __int64 84 | 85 | /* Define to a 64-bit unsigned integer type if one exists */ 86 | #define __libelf_u64_t unsigned __int64 87 | 88 | /* Define to a 32-bit signed integer type if one exists */ 89 | #define __libelf_i32_t int 90 | 91 | /* Define to a 32-bit unsigned integer type if one exists */ 92 | #define __libelf_u32_t unsigned int 93 | 94 | /* Define to a 16-bit signed integer type if one exists */ 95 | #define __libelf_i16_t short int 96 | 97 | /* Define to a 16-bit unsigned integer type if one exists */ 98 | #define __libelf_u16_t unsigned short int 99 | 100 | /* The number of bytes in a __int64. */ 101 | #define SIZEOF___INT64 8 102 | 103 | /* The number of bytes in a int. */ 104 | #define SIZEOF_INT 4 105 | 106 | /* The number of bytes in a long. */ 107 | #define SIZEOF_LONG 4 108 | 109 | /* The number of bytes in a long long. */ 110 | #define SIZEOF_LONG_LONG 0 111 | 112 | /* The number of bytes in a short. */ 113 | #define SIZEOF_SHORT 2 114 | 115 | /* Define if you have the ftruncate function. */ 116 | #undef HAVE_FTRUNCATE 117 | 118 | /* Define if you have the getpagesize function. */ 119 | #undef HAVE_GETPAGESIZE 120 | 121 | /* Define if you have the memcmp function. */ 122 | #define HAVE_MEMCMP 1 123 | 124 | /* Define if you have the memcpy function. */ 125 | #define HAVE_MEMCPY 1 126 | 127 | /* Define if you have the memmove function. */ 128 | #define HAVE_MEMMOVE 1 129 | 130 | /* Define if you have the memset function. */ 131 | #define HAVE_MEMSET 1 132 | 133 | /* Define if you have the header file. */ 134 | #undef HAVE_AR_H 135 | 136 | /* Define if you have the header file. */ 137 | #undef HAVE_ELF_H 138 | 139 | /* Define if you have the header file. */ 140 | #undef HAVE_FCNTL_H 141 | 142 | /* Define if you have the header file. */ 143 | #undef HAVE_GELF_H 144 | 145 | /* Define if you have the header file. */ 146 | #undef HAVE_LIBELF_H 147 | 148 | /* Define if you have the header file. */ 149 | #undef HAVE_LINK_H 150 | 151 | /* Define if you have the header file. */ 152 | #undef HAVE_NLIST_H 153 | 154 | /* Define if you have the header file. */ 155 | #undef HAVE_SYS_ELF_H 156 | 157 | /* Define if you have the header file. */ 158 | #undef HAVE_SYS_LINK_H 159 | 160 | /* Define if you have the header file. */ 161 | #undef HAVE_UNISTD_H 162 | -------------------------------------------------------------------------------- /lib/data.c: -------------------------------------------------------------------------------- 1 | /* 2 | * data.c - libelf internal variables. 3 | * Copyright (C) 1995 - 1998, 2007 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: data.c,v 1.8 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | unsigned _elf_version = EV_NONE; 27 | int _elf_errno = 0; 28 | int _elf_fill = 0; 29 | 30 | #if ENABLE_SANITY_CHECKS 31 | #define SANITY_CHECKS -1 32 | #else 33 | #define SANITY_CHECKS 0 34 | #endif 35 | 36 | int _elf_sanity_checks = SANITY_CHECKS; 37 | -------------------------------------------------------------------------------- /lib/end.c: -------------------------------------------------------------------------------- 1 | /* 2 | * end.c - implementation of the elf_end(3) function. 3 | * Copyright (C) 1995 - 2004 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: end.c,v 1.12 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | #if HAVE_MMAP 27 | #include 28 | #endif /* HAVE_MMAP */ 29 | 30 | static void 31 | _elf_free(void *ptr) { 32 | if (ptr) { 33 | free(ptr); 34 | } 35 | } 36 | 37 | static void 38 | _elf_free_scns(Elf *elf, Elf_Scn *scn) { 39 | Scn_Data *sd, *tmp; 40 | Elf_Scn *freescn; 41 | 42 | for (freescn = NULL; scn; scn = scn->s_link) { 43 | elf_assert(scn->s_magic == SCN_MAGIC); 44 | elf_assert(scn->s_elf == elf); 45 | for (sd = scn->s_data_1; sd; sd = tmp) { 46 | elf_assert(sd->sd_magic == DATA_MAGIC); 47 | elf_assert(sd->sd_scn == scn); 48 | tmp = sd->sd_link; 49 | if (sd->sd_free_data) { 50 | _elf_free(sd->sd_memdata); 51 | } 52 | if (sd->sd_freeme) { 53 | free(sd); 54 | } 55 | } 56 | if ((sd = scn->s_rawdata)) { 57 | elf_assert(sd->sd_magic == DATA_MAGIC); 58 | elf_assert(sd->sd_scn == scn); 59 | if (sd->sd_free_data) { 60 | _elf_free(sd->sd_memdata); 61 | } 62 | if (sd->sd_freeme) { 63 | free(sd); 64 | } 65 | } 66 | if (scn->s_freeme) { 67 | _elf_free(freescn); 68 | freescn = scn; 69 | } 70 | } 71 | _elf_free(freescn); 72 | } 73 | 74 | int 75 | elf_end(Elf *elf) { 76 | Elf **siblings; 77 | 78 | if (!elf) { 79 | return 0; 80 | } 81 | elf_assert(elf->e_magic == ELF_MAGIC); 82 | if (--elf->e_count) { 83 | return elf->e_count; 84 | } 85 | if (elf->e_parent) { 86 | elf_assert(elf->e_parent->e_magic == ELF_MAGIC); 87 | elf_assert(elf->e_parent->e_kind == ELF_K_AR); 88 | siblings = &elf->e_parent->e_members; 89 | while (*siblings) { 90 | if (*siblings == elf) { 91 | *siblings = elf->e_link; 92 | break; 93 | } 94 | siblings = &(*siblings)->e_link; 95 | } 96 | elf_end(elf->e_parent); 97 | _elf_free(elf->e_arhdr); 98 | } 99 | #if HAVE_MMAP 100 | else if (elf->e_unmap_data) { 101 | munmap(elf->e_data, elf->e_size); 102 | } 103 | #endif /* HAVE_MMAP */ 104 | else if (!elf->e_memory) { 105 | _elf_free(elf->e_data); 106 | } 107 | _elf_free_scns(elf, elf->e_scn_1); 108 | if (elf->e_rawdata != elf->e_data) { 109 | _elf_free(elf->e_rawdata); 110 | } 111 | if (elf->e_free_syms) { 112 | _elf_free(elf->e_symtab); 113 | } 114 | _elf_free(elf->e_ehdr); 115 | _elf_free(elf->e_phdr); 116 | free(elf); 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /lib/errmsg.c: -------------------------------------------------------------------------------- 1 | /* 2 | * errmsg.c - implementation of the elf_errmsg(3) function. 3 | * Copyright (C) 1995 - 1999, 2004 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: errmsg.c,v 1.11 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | #if HAVE_DGETTEXT 27 | # undef HAVE_CATGETS 28 | # include 29 | #else /* HAVE_DGETTEXT */ 30 | # define dgettext(dom, str) str 31 | #endif /* HAVE_DGETTEXT */ 32 | 33 | #if HAVE_CATGETS 34 | # include 35 | static nl_catd _libelf_cat = (nl_catd)0; 36 | #endif /* HAVE_CATGETS */ 37 | 38 | #if HAVE_DGETTEXT || HAVE_CATGETS 39 | static const char domain[] = "libelf"; 40 | #endif /* HAVE_DGETTEXT || HAVE_CATGETS */ 41 | 42 | #if PIC 43 | static const char *_messages[] = { 44 | #else /* PIC */ 45 | static const char *const _messages[] = { 46 | #endif /* PIC */ 47 | #define __err__(a,b) b, 48 | #include /* include string tables from errors.h */ 49 | #undef __err__ 50 | }; 51 | 52 | const char* 53 | elf_errmsg(int err) { 54 | if (err == 0) { 55 | err = _elf_errno; 56 | if (err == 0) { 57 | return NULL; 58 | } 59 | } 60 | else if (err == -1) { 61 | err = _elf_errno; 62 | } 63 | 64 | if (err < 0 || err >= ERROR_NUM || _messages[err] == NULL) { 65 | err = ERROR_UNKNOWN; 66 | } 67 | 68 | #if HAVE_CATGETS 69 | if (_libelf_cat == (nl_catd)0) { 70 | _libelf_cat = catopen(domain, 0); 71 | } 72 | if (_libelf_cat != (nl_catd)-1) { 73 | return catgets(_libelf_cat, 1, err + 1, _messages[err]); 74 | } 75 | #endif /* HAVE_CATGETS */ 76 | return dgettext(domain, _messages[err]); 77 | } 78 | -------------------------------------------------------------------------------- /lib/errno.c: -------------------------------------------------------------------------------- 1 | /* 2 | errno.c - implementation of the elf_errno(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: errno.c,v 1.7 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | int 27 | elf_errno(void) { 28 | int tmp = _elf_errno; 29 | 30 | _elf_errno = 0; 31 | return tmp; 32 | } 33 | -------------------------------------------------------------------------------- /lib/errors.h: -------------------------------------------------------------------------------- 1 | /* 2 | * errors.h - exhaustive list of all error codes and messages for libelf. 3 | * Copyright (C) 1995 - 2003, 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | /* @(#) $Id: errors.h,v 1.18 2008/05/23 08:15:34 michael Exp $ */ 21 | 22 | /* dummy for xgettext */ 23 | #define _(str) str 24 | 25 | __err__(ERROR_OK, _("no error")) 26 | __err__(ERROR_UNKNOWN, _("unknown error")) 27 | __err__(ERROR_INTERNAL, _("Internal error: unknown reason")) 28 | __err__(ERROR_UNIMPLEMENTED, _("Internal error: not implemented")) 29 | __err__(ERROR_WRONLY, _("Request error: cntl(ELF_C_FDREAD) on write-only file")) 30 | __err__(ERROR_INVALID_CMD, _("Request error: invalid ELF_C_* argument")) 31 | __err__(ERROR_FDDISABLED, _("Request error: file descriptor disabled")) 32 | __err__(ERROR_NOTARCHIVE, _("Request error: not an archive")) 33 | __err__(ERROR_BADOFF, _("Request error: offset out of range")) 34 | __err__(ERROR_UNKNOWN_VERSION, _("Request error: unknown ELF version")) 35 | __err__(ERROR_CMDMISMATCH, _("Request error: ELF_C_* argument does not match")) 36 | __err__(ERROR_MEMBERWRITE, _("Request error: archive member begin() for writing")) 37 | __err__(ERROR_FDMISMATCH, _("Request error: archive/member file descriptor mismatch")) 38 | __err__(ERROR_NOTELF, _("Request error: not an ELF file")) 39 | __err__(ERROR_CLASSMISMATCH, _("Request error: class file/memory mismatch")) 40 | __err__(ERROR_UNKNOWN_TYPE, _("Request error: invalid ELF_T_* argument")) 41 | __err__(ERROR_UNKNOWN_ENCODING, _("Request error: unknown data encoding")) 42 | __err__(ERROR_DST2SMALL, _("Request error: destination buffer too small")) 43 | __err__(ERROR_NULLBUF, _("Request error: d_buf is NULL")) 44 | __err__(ERROR_UNKNOWN_CLASS, _("Request error: unknown ELF class")) 45 | __err__(ERROR_ELFSCNMISMATCH, _("Request error: section does not belong to file")) 46 | __err__(ERROR_NOSUCHSCN, _("Request error: no section at index")) 47 | __err__(ERROR_NULLSCN, _("Request error: can't manipulate null section")) 48 | __err__(ERROR_SCNDATAMISMATCH, _("Request error: data does not belong to section")) 49 | __err__(ERROR_NOSTRTAB, _("Request error: no string table")) 50 | __err__(ERROR_BADSTROFF, _("Request error: string table offset out of range")) 51 | __err__(ERROR_RDONLY, _("Request error: update(ELF_C_WRITE) on read-only file")) 52 | __err__(ERROR_IO_SEEK, _("I/O error: seek")) 53 | __err__(ERROR_IO_2BIG, _("I/O error: file too big for memory")) 54 | __err__(ERROR_IO_READ, _("I/O error: raw read")) 55 | __err__(ERROR_IO_GETSIZE, _("I/O error: get file size")) 56 | __err__(ERROR_IO_WRITE, _("I/O error: output write")) 57 | __err__(ERROR_IO_TRUNC, _("I/O error: can't truncate output file")) 58 | __err__(ERROR_VERSION_UNSET, _("Sequence error: must set ELF version first")) 59 | __err__(ERROR_NOEHDR, _("Sequence error: must create ELF header first")) 60 | __err__(ERROR_OUTSIDE, _("Format error: reference outside file")) 61 | __err__(ERROR_TRUNC_ARHDR, _("Format error: archive header truncated")) 62 | __err__(ERROR_ARFMAG, _("Format error: archive fmag")) 63 | __err__(ERROR_ARHDR, _("Format error: archive header")) 64 | __err__(ERROR_TRUNC_MEMBER, _("Format error: archive member truncated")) 65 | __err__(ERROR_SIZE_ARSYMTAB, _("Format error: archive symbol table size")) 66 | __err__(ERROR_ARSTRTAB, _("Format error: archive string table")) 67 | __err__(ERROR_ARSPECIAL, _("Format error: archive special name unknown")) 68 | __err__(ERROR_TRUNC_EHDR, _("Format error: ELF header truncated")) 69 | __err__(ERROR_TRUNC_PHDR, _("Format error: program header table truncated")) 70 | __err__(ERROR_TRUNC_SHDR, _("Format error: section header table truncated")) 71 | __err__(ERROR_TRUNC_SCN, _("Format error: data region truncated")) 72 | __err__(ERROR_ALIGN_PHDR, _("Format error: program header table alignment")) 73 | __err__(ERROR_ALIGN_SHDR, _("Format error: section header table alignment")) 74 | __err__(ERROR_VERDEF_FORMAT, _("Format error: bad parameter in Verdef record")) 75 | __err__(ERROR_VERDEF_VERSION, _("Format error: unknown Verdef version")) 76 | __err__(ERROR_VERNEED_FORMAT, _("Format error: bad parameter in Verneed record")) 77 | __err__(ERROR_VERNEED_VERSION, _("Format error: unknown Verneed version")) 78 | __err__(ERROR_EHDR_SHNUM, _("Format error: bad e_shnum value")) 79 | __err__(ERROR_EHDR_SHENTSIZE, _("Format error: bad e_shentsize value")) 80 | __err__(ERROR_EHDR_PHENTSIZE, _("Format error: bad e_phentsize value")) 81 | __err__(ERROR_UNTERM, _("Format error: unterminated string in string table")) 82 | __err__(ERROR_SCN2SMALL, _("Layout error: section size too small for data")) 83 | __err__(ERROR_SCN_OVERLAP, _("Layout error: overlapping sections")) 84 | __err__(ERROR_MEM_ELF, _("Memory error: elf descriptor")) 85 | __err__(ERROR_MEM_ARSYMTAB, _("Memory error: archive symbol table")) 86 | __err__(ERROR_MEM_ARHDR, _("Memory error: archive member header")) 87 | __err__(ERROR_MEM_EHDR, _("Memory error: ELF header")) 88 | __err__(ERROR_MEM_PHDR, _("Memory error: program header table")) 89 | __err__(ERROR_MEM_SHDR, _("Memory error: section header table")) 90 | __err__(ERROR_MEM_SCN, _("Memory error: section descriptor")) 91 | __err__(ERROR_MEM_SCNDATA, _("Memory error: section data")) 92 | __err__(ERROR_MEM_OUTBUF, _("Memory error: output file space")) 93 | __err__(ERROR_MEM_TEMPORARY, _("Memory error: temporary buffer")) 94 | __err__(ERROR_BADVALUE, _("GElf error: value out of range")) 95 | __err__(ERROR_BADINDEX, _("GElf error: index out of range")) 96 | __err__(ERROR_BADTYPE, _("GElf error: type mismatch")) 97 | __err__(ERROR_MEM_SYM, _("GElf error: not enough memory for GElf_Sym")) 98 | __err__(ERROR_MEM_DYN, _("GElf error: not enough memory for GElf_Dyn")) 99 | __err__(ERROR_MEM_RELA, _("GElf error: not enough memory for GElf_Rela")) 100 | __err__(ERROR_MEM_REL, _("GElf error: not enough memory for GElf_Rel")) 101 | -------------------------------------------------------------------------------- /lib/fill.c: -------------------------------------------------------------------------------- 1 | /* 2 | fill.c - implementation of the elf_fill(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: fill.c,v 1.7 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | void 27 | elf_fill(int fill) { 28 | _elf_fill = fill; 29 | } 30 | -------------------------------------------------------------------------------- /lib/flag.c: -------------------------------------------------------------------------------- 1 | /* 2 | flag.c - implementation of the elf_flag*(3) functions. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: flag.c,v 1.7 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | static unsigned 27 | _elf_flag(unsigned *f, Elf_Cmd cmd, unsigned flags) { 28 | if (cmd == ELF_C_SET) { 29 | return *f |= flags; 30 | } 31 | if (cmd == ELF_C_CLR) { 32 | return *f &= ~flags; 33 | } 34 | seterr(ERROR_INVALID_CMD); 35 | return 0; 36 | } 37 | 38 | unsigned 39 | elf_flagdata(Elf_Data *data, Elf_Cmd cmd, unsigned flags) { 40 | Scn_Data *sd = (Scn_Data*)data; 41 | 42 | if (!sd) { 43 | return 0; 44 | } 45 | elf_assert(sd->sd_magic == DATA_MAGIC); 46 | return _elf_flag(&sd->sd_data_flags, cmd, flags); 47 | } 48 | 49 | unsigned 50 | elf_flagehdr(Elf *elf, Elf_Cmd cmd, unsigned flags) { 51 | if (!elf) { 52 | return 0; 53 | } 54 | elf_assert(elf->e_magic == ELF_MAGIC); 55 | return _elf_flag(&elf->e_ehdr_flags, cmd, flags); 56 | } 57 | 58 | unsigned 59 | elf_flagelf(Elf *elf, Elf_Cmd cmd, unsigned flags) { 60 | if (!elf) { 61 | return 0; 62 | } 63 | elf_assert(elf->e_magic == ELF_MAGIC); 64 | return _elf_flag(&elf->e_elf_flags, cmd, flags); 65 | } 66 | 67 | unsigned 68 | elf_flagphdr(Elf *elf, Elf_Cmd cmd, unsigned flags) { 69 | if (!elf) { 70 | return 0; 71 | } 72 | elf_assert(elf->e_magic == ELF_MAGIC); 73 | return _elf_flag(&elf->e_phdr_flags, cmd, flags); 74 | } 75 | 76 | unsigned 77 | elf_flagscn(Elf_Scn *scn, Elf_Cmd cmd, unsigned flags) { 78 | if (!scn) { 79 | return 0; 80 | } 81 | elf_assert(scn->s_magic == SCN_MAGIC); 82 | return _elf_flag(&scn->s_scn_flags, cmd, flags); 83 | } 84 | 85 | unsigned 86 | elf_flagshdr(Elf_Scn *scn, Elf_Cmd cmd, unsigned flags) { 87 | if (!scn) { 88 | return 0; 89 | } 90 | elf_assert(scn->s_magic == SCN_MAGIC); 91 | return _elf_flag(&scn->s_shdr_flags, cmd, flags); 92 | } 93 | -------------------------------------------------------------------------------- /lib/gelf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * gelf.h - public header file for libelf. 3 | * Copyright (C) 2000 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | /* @(#) $Id: gelf.h,v 1.16 2008/05/23 08:15:34 michael Exp $ */ 21 | 22 | #ifndef _GELF_H 23 | #define _GELF_H 24 | 25 | #if __LIBELF_INTERNAL__ 26 | #include 27 | #else /* __LIBELF_INTERNAL__ */ 28 | #include 29 | #endif /* __LIBELF_INTERNAL__ */ 30 | 31 | #if __LIBELF_NEED_LINK_H 32 | #include 33 | #elif __LIBELF_NEED_SYS_LINK_H 34 | #include 35 | #endif /* __LIBELF_NEED_LINK_H */ 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif /* __cplusplus */ 40 | 41 | #ifndef __P 42 | # if (__STDC__ + 0) || defined(__cplusplus) || defined(_WIN32) 43 | # define __P(args) args 44 | # else /* __STDC__ || defined(__cplusplus) */ 45 | # define __P(args) () 46 | # endif /* __STDC__ || defined(__cplusplus) */ 47 | #endif /* __P */ 48 | 49 | #if !__LIBELF64 50 | 51 | #error "GElf is not supported on this system." 52 | 53 | #else /* __LIBELF64 */ 54 | 55 | typedef Elf64_Addr GElf_Addr; 56 | typedef Elf64_Half GElf_Half; 57 | typedef Elf64_Off GElf_Off; 58 | typedef Elf64_Sword GElf_Sword; 59 | typedef Elf64_Word GElf_Word; 60 | typedef Elf64_Sxword GElf_Sxword; 61 | typedef Elf64_Xword GElf_Xword; 62 | 63 | typedef Elf64_Ehdr GElf_Ehdr; 64 | typedef Elf64_Phdr GElf_Phdr; 65 | typedef Elf64_Shdr GElf_Shdr; 66 | typedef Elf64_Dyn GElf_Dyn; 67 | typedef Elf64_Rel GElf_Rel; 68 | typedef Elf64_Rela GElf_Rela; 69 | typedef Elf64_Sym GElf_Sym; 70 | 71 | /* 72 | * Symbol versioning 73 | */ 74 | #if __LIBELF_SYMBOL_VERSIONS 75 | typedef Elf64_Verdef GElf_Verdef; 76 | typedef Elf64_Verneed GElf_Verneed; 77 | typedef Elf64_Verdaux GElf_Verdaux; 78 | typedef Elf64_Vernaux GElf_Vernaux; 79 | #endif /* __LIBELF_SYMBOL_VERSIONS */ 80 | 81 | /* 82 | * These types aren't implemented (yet) 83 | * 84 | typedef Elf64_Move GElf_Move; 85 | typedef Elf64_Syminfo GElf_Syminfo; 86 | */ 87 | 88 | /* 89 | * Generic macros 90 | */ 91 | #define GELF_ST_BIND ELF64_ST_BIND 92 | #define GELF_ST_TYPE ELF64_ST_TYPE 93 | #define GELF_ST_INFO ELF64_ST_INFO 94 | 95 | #define GELF_R_TYPE ELF64_R_TYPE 96 | #define GELF_R_SYM ELF64_R_SYM 97 | #define GELF_R_INFO ELF64_R_INFO 98 | 99 | /* 100 | * Function declarations 101 | */ 102 | extern int gelf_getclass __P((Elf *__elf)); 103 | 104 | extern size_t gelf_fsize __P((Elf *__elf, Elf_Type __type, size_t __count, unsigned __ver)); 105 | 106 | extern Elf_Data *gelf_xlatetof __P((Elf *__elf, Elf_Data *__dst, const Elf_Data *__src, unsigned __encode)); 107 | extern Elf_Data *gelf_xlatetom __P((Elf *__elf, Elf_Data *__dst, const Elf_Data *__src, unsigned __encode)); 108 | 109 | extern GElf_Ehdr *gelf_getehdr __P((Elf *__elf, GElf_Ehdr *__dst)); 110 | extern int gelf_update_ehdr __P((Elf *__elf, GElf_Ehdr *__src)); 111 | extern unsigned long gelf_newehdr __P((Elf *__elf, int __elfclass)); 112 | 113 | extern GElf_Phdr *gelf_getphdr __P((Elf *__elf, int ndx, GElf_Phdr *__dst)); 114 | extern int gelf_update_phdr __P((Elf *__elf, int ndx, GElf_Phdr *__src)); 115 | extern unsigned long gelf_newphdr __P((Elf *__elf, size_t __phnum)); 116 | 117 | extern GElf_Shdr *gelf_getshdr __P((Elf_Scn *__scn, GElf_Shdr *__dst)); 118 | extern int gelf_update_shdr __P((Elf_Scn *__scn, GElf_Shdr *__src)); 119 | 120 | extern GElf_Dyn *gelf_getdyn __P((Elf_Data *__src, int __ndx, GElf_Dyn *__dst)); 121 | extern int gelf_update_dyn __P((Elf_Data *__dst, int __ndx, GElf_Dyn *__src)); 122 | 123 | extern GElf_Rel *gelf_getrel __P((Elf_Data *__src, int __ndx, GElf_Rel *__dst)); 124 | extern int gelf_update_rel __P((Elf_Data *__dst, int __ndx, GElf_Rel *__src)); 125 | 126 | extern GElf_Rela *gelf_getrela __P((Elf_Data *__src, int __ndx, GElf_Rela *__dst)); 127 | extern int gelf_update_rela __P((Elf_Data *__dst, int __ndx, GElf_Rela *__src)); 128 | 129 | extern GElf_Sym *gelf_getsym __P((Elf_Data *__src, int __ndx, GElf_Sym *__dst)); 130 | extern int gelf_update_sym __P((Elf_Data *__dst, int __ndx, GElf_Sym *__src)); 131 | 132 | extern long gelf_checksum __P((Elf *__elf)); 133 | 134 | /* 135 | * These functions aren't implemented (yet) 136 | * 137 | extern GElf_Move *gelf_getmove __P((Elf_Data *__src, int __ndx, GElf_Move *__src)); 138 | extern int gelf_update_move __P((Elf_Data *__dst, int __ndx, GElf_Move *__src)); 139 | * 140 | extern GElf_Syminfo* gelf_getsyminfo __P((Elf_Data *__src, int __ndx, GElf_Syminfo *__dst)); 141 | extern int gelf_update_syminfo __P((Elf_Data *__dst, int __ndx, GElf_Syminfo *__src)); 142 | */ 143 | 144 | /* 145 | * Extensions (not available in other versions of libelf) 146 | */ 147 | extern size_t gelf_msize __P((Elf *__elf, Elf_Type __type, size_t __count, unsigned __ver)); 148 | 149 | #endif /* __LIBELF64 */ 150 | 151 | #ifdef __cplusplus 152 | } 153 | #endif /* __cplusplus */ 154 | 155 | #endif /* _GELF_H */ 156 | -------------------------------------------------------------------------------- /lib/gelfehdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gelfehdr.c - gelf_* translation functions. 3 | * Copyright (C) 2000 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #if __LIBELF64 23 | 24 | #ifndef lint 25 | static const char rcsid[] = "@(#) $Id: gelfehdr.c,v 1.9 2008/05/23 08:15:34 michael Exp $"; 26 | #endif /* lint */ 27 | 28 | #define check_and_copy(type, d, s, name, eret) \ 29 | do { \ 30 | if (sizeof((d)->name) < sizeof((s)->name) \ 31 | && (type)(s)->name != (s)->name) { \ 32 | seterr(ERROR_BADVALUE); \ 33 | return (eret); \ 34 | } \ 35 | (d)->name = (type)(s)->name; \ 36 | } while (0) 37 | 38 | GElf_Ehdr* 39 | gelf_getehdr(Elf *elf, GElf_Ehdr *dst) { 40 | GElf_Ehdr buf; 41 | char *tmp; 42 | 43 | if (!elf) { 44 | return NULL; 45 | } 46 | elf_assert(elf->e_magic == ELF_MAGIC); 47 | tmp = _elf_getehdr(elf, elf->e_class); 48 | if (!tmp) { 49 | return NULL; 50 | } 51 | if (!dst) { 52 | dst = &buf; 53 | } 54 | if (elf->e_class == ELFCLASS64) { 55 | *dst = *(Elf64_Ehdr*)tmp; 56 | } 57 | else if (elf->e_class == ELFCLASS32) { 58 | Elf32_Ehdr *src = (Elf32_Ehdr*)tmp; 59 | 60 | memcpy(dst->e_ident, src->e_ident, EI_NIDENT); 61 | check_and_copy(GElf_Half, dst, src, e_type, NULL); 62 | check_and_copy(GElf_Half, dst, src, e_machine, NULL); 63 | check_and_copy(GElf_Word, dst, src, e_version, NULL); 64 | check_and_copy(GElf_Addr, dst, src, e_entry, NULL); 65 | check_and_copy(GElf_Off, dst, src, e_phoff, NULL); 66 | check_and_copy(GElf_Off, dst, src, e_shoff, NULL); 67 | check_and_copy(GElf_Word, dst, src, e_flags, NULL); 68 | check_and_copy(GElf_Half, dst, src, e_ehsize, NULL); 69 | check_and_copy(GElf_Half, dst, src, e_phentsize, NULL); 70 | check_and_copy(GElf_Half, dst, src, e_phnum, NULL); 71 | check_and_copy(GElf_Half, dst, src, e_shentsize, NULL); 72 | check_and_copy(GElf_Half, dst, src, e_shnum, NULL); 73 | check_and_copy(GElf_Half, dst, src, e_shstrndx, NULL); 74 | } 75 | else { 76 | if (valid_class(elf->e_class)) { 77 | seterr(ERROR_UNIMPLEMENTED); 78 | } 79 | else { 80 | seterr(ERROR_UNKNOWN_CLASS); 81 | } 82 | return NULL; 83 | } 84 | if (dst == &buf) { 85 | dst = (GElf_Ehdr*)malloc(sizeof(GElf_Ehdr)); 86 | if (!dst) { 87 | seterr(ERROR_MEM_EHDR); 88 | return NULL; 89 | } 90 | *dst = buf; 91 | } 92 | return dst; 93 | } 94 | 95 | int 96 | gelf_update_ehdr(Elf *elf, GElf_Ehdr *src) { 97 | char *tmp; 98 | 99 | if (!elf || !src) { 100 | return 0; 101 | } 102 | elf_assert(elf->e_magic == ELF_MAGIC); 103 | tmp = _elf_getehdr(elf, elf->e_class); 104 | if (!tmp) { 105 | return 0; 106 | } 107 | if (elf->e_class == ELFCLASS64) { 108 | *(Elf64_Ehdr*)tmp = *src; 109 | } 110 | else if (elf->e_class == ELFCLASS32) { 111 | Elf32_Ehdr *dst = (Elf32_Ehdr*)tmp; 112 | 113 | memcpy(dst->e_ident, src->e_ident, EI_NIDENT); 114 | check_and_copy(Elf32_Half, dst, src, e_type, 0); 115 | check_and_copy(Elf32_Half, dst, src, e_machine, 0); 116 | check_and_copy(Elf32_Word, dst, src, e_version, 0); 117 | check_and_copy(Elf32_Addr, dst, src, e_entry, 0); 118 | check_and_copy(Elf32_Off, dst, src, e_phoff, 0); 119 | check_and_copy(Elf32_Off, dst, src, e_shoff, 0); 120 | check_and_copy(Elf32_Word, dst, src, e_flags, 0); 121 | check_and_copy(Elf32_Half, dst, src, e_ehsize, 0); 122 | check_and_copy(Elf32_Half, dst, src, e_phentsize, 0); 123 | check_and_copy(Elf32_Half, dst, src, e_phnum, 0); 124 | check_and_copy(Elf32_Half, dst, src, e_shentsize, 0); 125 | check_and_copy(Elf32_Half, dst, src, e_shnum, 0); 126 | check_and_copy(Elf32_Half, dst, src, e_shstrndx, 0); 127 | } 128 | else { 129 | if (valid_class(elf->e_class)) { 130 | seterr(ERROR_UNIMPLEMENTED); 131 | } 132 | else { 133 | seterr(ERROR_UNKNOWN_CLASS); 134 | } 135 | return 0; 136 | } 137 | return 1; 138 | } 139 | 140 | #endif /* __LIBELF64 */ 141 | -------------------------------------------------------------------------------- /lib/gelfphdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gelfphdr.c - gelf_* translation functions. 3 | * Copyright (C) 2000 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #if __LIBELF64 23 | 24 | #ifndef lint 25 | static const char rcsid[] = "@(#) $Id: gelfphdr.c,v 1.9 2008/05/23 08:15:34 michael Exp $"; 26 | #endif /* lint */ 27 | 28 | #define check_and_copy(type, d, s, name, eret) \ 29 | do { \ 30 | if (sizeof((d)->name) < sizeof((s)->name) \ 31 | && (type)(s)->name != (s)->name) { \ 32 | seterr(ERROR_BADVALUE); \ 33 | return (eret); \ 34 | } \ 35 | (d)->name = (type)(s)->name; \ 36 | } while (0) 37 | 38 | GElf_Phdr* 39 | gelf_getphdr(Elf *elf, int ndx, GElf_Phdr *dst) { 40 | GElf_Phdr buf; 41 | char *tmp; 42 | size_t n; 43 | 44 | if (!elf) { 45 | return NULL; 46 | } 47 | elf_assert(elf->e_magic == ELF_MAGIC); 48 | tmp = _elf_getphdr(elf, elf->e_class); 49 | if (!tmp) { 50 | return NULL; 51 | } 52 | if (ndx < 0 || ndx >= elf->e_phnum) { 53 | seterr(ERROR_BADINDEX); 54 | return NULL; 55 | } 56 | n = _msize(elf->e_class, _elf_version, ELF_T_PHDR); 57 | if (n == 0) { 58 | seterr(ERROR_UNIMPLEMENTED); 59 | return NULL; 60 | } 61 | if (!dst) { 62 | dst = &buf; 63 | } 64 | if (elf->e_class == ELFCLASS64) { 65 | *dst = *(Elf64_Phdr*)(tmp + ndx * n); 66 | } 67 | else if (elf->e_class == ELFCLASS32) { 68 | Elf32_Phdr *src = (Elf32_Phdr*)(tmp + ndx * n); 69 | 70 | check_and_copy(GElf_Word, dst, src, p_type, NULL); 71 | check_and_copy(GElf_Word, dst, src, p_flags, NULL); 72 | check_and_copy(GElf_Off, dst, src, p_offset, NULL); 73 | check_and_copy(GElf_Addr, dst, src, p_vaddr, NULL); 74 | check_and_copy(GElf_Addr, dst, src, p_paddr, NULL); 75 | check_and_copy(GElf_Xword, dst, src, p_filesz, NULL); 76 | check_and_copy(GElf_Xword, dst, src, p_memsz, NULL); 77 | check_and_copy(GElf_Xword, dst, src, p_align, NULL); 78 | } 79 | else { 80 | if (valid_class(elf->e_class)) { 81 | seterr(ERROR_UNIMPLEMENTED); 82 | } 83 | else { 84 | seterr(ERROR_UNKNOWN_CLASS); 85 | } 86 | return NULL; 87 | } 88 | if (dst == &buf) { 89 | dst = (GElf_Phdr*)malloc(sizeof(GElf_Phdr)); 90 | if (!dst) { 91 | seterr(ERROR_MEM_PHDR); 92 | return NULL; 93 | } 94 | *dst = buf; 95 | } 96 | return dst; 97 | } 98 | 99 | int 100 | gelf_update_phdr(Elf *elf, int ndx, GElf_Phdr *src) { 101 | char *tmp; 102 | size_t n; 103 | 104 | if (!elf || !src) { 105 | return 0; 106 | } 107 | elf_assert(elf->e_magic == ELF_MAGIC); 108 | tmp = _elf_getphdr(elf, elf->e_class); 109 | if (!tmp) { 110 | return 0; 111 | } 112 | if (ndx < 0 || ndx >= elf->e_phnum) { 113 | seterr(ERROR_BADINDEX); 114 | return 0; 115 | } 116 | n = _msize(elf->e_class, _elf_version, ELF_T_PHDR); 117 | if (n == 0) { 118 | seterr(ERROR_UNIMPLEMENTED); 119 | return 0; 120 | } 121 | if (elf->e_class == ELFCLASS64) { 122 | *(Elf64_Phdr*)(tmp + ndx * n) = *src; 123 | } 124 | else if (elf->e_class == ELFCLASS32) { 125 | Elf32_Phdr *dst = (Elf32_Phdr*)(tmp + ndx * n); 126 | 127 | check_and_copy(Elf32_Word, dst, src, p_type, 0); 128 | check_and_copy(Elf32_Off, dst, src, p_offset, 0); 129 | check_and_copy(Elf32_Addr, dst, src, p_vaddr, 0); 130 | check_and_copy(Elf32_Addr, dst, src, p_paddr, 0); 131 | check_and_copy(Elf32_Word, dst, src, p_filesz, 0); 132 | check_and_copy(Elf32_Word, dst, src, p_memsz, 0); 133 | check_and_copy(Elf32_Word, dst, src, p_flags, 0); 134 | check_and_copy(Elf32_Word, dst, src, p_align, 0); 135 | } 136 | else { 137 | if (valid_class(elf->e_class)) { 138 | seterr(ERROR_UNIMPLEMENTED); 139 | } 140 | else { 141 | seterr(ERROR_UNKNOWN_CLASS); 142 | } 143 | return 0; 144 | } 145 | return 1; 146 | } 147 | 148 | #endif /* __LIBELF64 */ 149 | -------------------------------------------------------------------------------- /lib/gelfshdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gelfshdr.c - gelf_* translation functions. 3 | * Copyright (C) 2000 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #if __LIBELF64 23 | 24 | #ifndef lint 25 | static const char rcsid[] = "@(#) $Id: gelfshdr.c,v 1.10 2008/05/23 08:15:34 michael Exp $"; 26 | #endif /* lint */ 27 | 28 | #define check_and_copy(type, d, s, name, eret) \ 29 | do { \ 30 | if (sizeof((d)->name) < sizeof((s)->name) \ 31 | && (type)(s)->name != (s)->name) { \ 32 | seterr(ERROR_BADVALUE); \ 33 | return (eret); \ 34 | } \ 35 | (d)->name = (type)(s)->name; \ 36 | } while (0) 37 | 38 | GElf_Shdr* 39 | gelf_getshdr(Elf_Scn *scn, GElf_Shdr *dst) { 40 | GElf_Shdr buf; 41 | 42 | if (!scn) { 43 | return NULL; 44 | } 45 | elf_assert(scn->s_magic == SCN_MAGIC); 46 | elf_assert(scn->s_elf); 47 | elf_assert(scn->s_elf->e_magic == ELF_MAGIC); 48 | if (!dst) { 49 | dst = &buf; 50 | } 51 | if (scn->s_elf->e_class == ELFCLASS64) { 52 | *dst = scn->s_shdr64; 53 | } 54 | else if (scn->s_elf->e_class == ELFCLASS32) { 55 | Elf32_Shdr *src = &scn->s_shdr32; 56 | 57 | check_and_copy(GElf_Word, dst, src, sh_name, NULL); 58 | check_and_copy(GElf_Word, dst, src, sh_type, NULL); 59 | check_and_copy(GElf_Xword, dst, src, sh_flags, NULL); 60 | check_and_copy(GElf_Addr, dst, src, sh_addr, NULL); 61 | check_and_copy(GElf_Off, dst, src, sh_offset, NULL); 62 | check_and_copy(GElf_Xword, dst, src, sh_size, NULL); 63 | check_and_copy(GElf_Word, dst, src, sh_link, NULL); 64 | check_and_copy(GElf_Word, dst, src, sh_info, NULL); 65 | check_and_copy(GElf_Xword, dst, src, sh_addralign, NULL); 66 | check_and_copy(GElf_Xword, dst, src, sh_entsize, NULL); 67 | } 68 | else { 69 | if (valid_class(scn->s_elf->e_class)) { 70 | seterr(ERROR_UNIMPLEMENTED); 71 | } 72 | else { 73 | seterr(ERROR_UNKNOWN_CLASS); 74 | } 75 | return NULL; 76 | } 77 | if (dst == &buf) { 78 | dst = (GElf_Shdr*)malloc(sizeof(GElf_Shdr)); 79 | if (!dst) { 80 | seterr(ERROR_MEM_SHDR); 81 | return NULL; 82 | } 83 | *dst = buf; 84 | } 85 | return dst; 86 | } 87 | 88 | int 89 | gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *src) { 90 | if (!scn || !src) { 91 | return 0; 92 | } 93 | elf_assert(scn->s_magic == SCN_MAGIC); 94 | elf_assert(scn->s_elf); 95 | elf_assert(scn->s_elf->e_magic == ELF_MAGIC); 96 | if (scn->s_elf->e_class == ELFCLASS64) { 97 | scn->s_shdr64 = *src; 98 | } 99 | else if (scn->s_elf->e_class == ELFCLASS32) { 100 | Elf32_Shdr *dst = &scn->s_shdr32; 101 | 102 | check_and_copy(Elf32_Word, dst, src, sh_name, 0); 103 | check_and_copy(Elf32_Word, dst, src, sh_type, 0); 104 | check_and_copy(Elf32_Word, dst, src, sh_flags, 0); 105 | check_and_copy(Elf32_Addr, dst, src, sh_addr, 0); 106 | check_and_copy(Elf32_Off, dst, src, sh_offset, 0); 107 | check_and_copy(Elf32_Word, dst, src, sh_size, 0); 108 | check_and_copy(Elf32_Word, dst, src, sh_link, 0); 109 | check_and_copy(Elf32_Word, dst, src, sh_info, 0); 110 | check_and_copy(Elf32_Word, dst, src, sh_addralign, 0); 111 | check_and_copy(Elf32_Word, dst, src, sh_entsize, 0); 112 | } 113 | else { 114 | if (valid_class(scn->s_elf->e_class)) { 115 | seterr(ERROR_UNIMPLEMENTED); 116 | } 117 | else { 118 | seterr(ERROR_UNKNOWN_CLASS); 119 | } 120 | return 0; 121 | } 122 | return 1; 123 | } 124 | 125 | #endif /* __LIBELF64 */ 126 | -------------------------------------------------------------------------------- /lib/getarhdr.c: -------------------------------------------------------------------------------- 1 | /* 2 | getarhdr.c - implementation of the elf_getarhdr(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: getarhdr.c,v 1.7 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | Elf_Arhdr* 27 | elf_getarhdr(Elf *elf) { 28 | if (!elf) { 29 | return NULL; 30 | } 31 | elf_assert(elf->e_magic == ELF_MAGIC); 32 | if (elf->e_arhdr) { 33 | return elf->e_arhdr; 34 | } 35 | seterr(ERROR_NOTARCHIVE); 36 | return NULL; 37 | } 38 | -------------------------------------------------------------------------------- /lib/getarsym.c: -------------------------------------------------------------------------------- 1 | /* 2 | * getarsym.c - implementation of the elf_getarsym(3) function. 3 | * Copyright (C) 1995 - 1998, 2004 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #ifndef lint 24 | static const char rcsid[] = "@(#) $Id: getarsym.c,v 1.9 2008/05/23 08:15:34 michael Exp $"; 25 | #endif /* lint */ 26 | 27 | Elf_Arsym* 28 | elf_getarsym(Elf *elf, size_t *ptr) { 29 | Elf_Arsym *syms; 30 | size_t count; 31 | size_t tmp; 32 | size_t i; 33 | char *s; 34 | char *e; 35 | 36 | if (!ptr) { 37 | ptr = &tmp; 38 | } 39 | *ptr = 0; 40 | if (!elf) { 41 | return NULL; 42 | } 43 | elf_assert(elf->e_magic == ELF_MAGIC); 44 | if (elf->e_kind != ELF_K_AR) { 45 | seterr(ERROR_NOTARCHIVE); 46 | return NULL; 47 | } 48 | if (elf->e_symtab && !elf->e_free_syms) { 49 | if (elf->e_symlen < 4) { 50 | seterr(ERROR_SIZE_ARSYMTAB); 51 | return NULL; 52 | } 53 | count = __load_u32M(elf->e_symtab); 54 | if (elf->e_symlen < 4 * (count + 1)) { 55 | seterr(ERROR_SIZE_ARSYMTAB); 56 | return NULL; 57 | } 58 | if (!(syms = (Elf_Arsym*)malloc((count + 1) * sizeof(*syms)))) { 59 | seterr(ERROR_MEM_ARSYMTAB); 60 | return NULL; 61 | } 62 | s = elf->e_symtab + 4 * (count + 1); 63 | e = elf->e_symtab + elf->e_symlen; 64 | for (i = 0; i < count; i++, s++) { 65 | syms[i].as_name = s; 66 | while (s < e && *s) { 67 | s++; 68 | } 69 | if (s >= e) { 70 | seterr(ERROR_SIZE_ARSYMTAB); 71 | free(syms); 72 | return NULL; 73 | } 74 | elf_assert(!*s); 75 | syms[i].as_hash = elf_hash((unsigned char*)syms[i].as_name); 76 | syms[i].as_off = __load_u32M(elf->e_symtab + 4 * (i + 1)); 77 | } 78 | syms[count].as_name = NULL; 79 | syms[count].as_hash = ~0UL; 80 | syms[count].as_off = 0; 81 | elf->e_symtab = (char*)syms; 82 | elf->e_symlen = count + 1; 83 | elf->e_free_syms = 1; 84 | } 85 | *ptr = elf->e_symlen; 86 | return (Elf_Arsym*)elf->e_symtab; 87 | } 88 | -------------------------------------------------------------------------------- /lib/getbase.c: -------------------------------------------------------------------------------- 1 | /* 2 | getbase.c - implementation of the elf_getbase(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: getbase.c,v 1.7 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | off_t 27 | elf_getbase(Elf *elf) { 28 | if (!elf) { 29 | return -1; 30 | } 31 | elf_assert(elf->e_magic == ELF_MAGIC); 32 | return (off_t)elf->e_base; 33 | } 34 | -------------------------------------------------------------------------------- /lib/getdata.c: -------------------------------------------------------------------------------- 1 | /* 2 | getdata.c - implementation of the elf_getdata(3) function. 3 | Copyright (C) 1995 - 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: getdata.c,v 1.13 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | static Elf_Data* 27 | _elf_cook_scn(Elf *elf, Elf_Scn *scn, Scn_Data *sd) { 28 | Elf_Data dst; 29 | Elf_Data src; 30 | int flag = 0; 31 | size_t dlen; 32 | 33 | elf_assert(elf->e_data); 34 | 35 | /* 36 | * Prepare source 37 | */ 38 | src = sd->sd_data; 39 | src.d_version = elf->e_version; 40 | if (elf->e_rawdata) { 41 | src.d_buf = elf->e_rawdata + scn->s_offset; 42 | } 43 | else { 44 | src.d_buf = elf->e_data + scn->s_offset; 45 | } 46 | 47 | /* 48 | * Prepare destination (needs prepared source!) 49 | */ 50 | dst = sd->sd_data; 51 | if (elf->e_class == ELFCLASS32) { 52 | dlen = _elf32_xltsize(&src, dst.d_version, elf->e_encoding, 0); 53 | } 54 | #if __LIBELF64 55 | else if (elf->e_class == ELFCLASS64) { 56 | dlen = _elf64_xltsize(&src, dst.d_version, elf->e_encoding, 0); 57 | } 58 | #endif /* __LIBELF64 */ 59 | else { 60 | elf_assert(valid_class(elf->e_class)); 61 | seterr(ERROR_UNIMPLEMENTED); 62 | return NULL; 63 | } 64 | if (dlen == (size_t)-1) { 65 | return NULL; 66 | } 67 | dst.d_size = dlen; 68 | if (elf->e_rawdata != elf->e_data && dst.d_size <= src.d_size) { 69 | dst.d_buf = elf->e_data + scn->s_offset; 70 | } 71 | else if (!(dst.d_buf = malloc(dst.d_size))) { 72 | seterr(ERROR_MEM_SCNDATA); 73 | return NULL; 74 | } 75 | else { 76 | flag = 1; 77 | } 78 | 79 | /* 80 | * Translate data 81 | */ 82 | if (_elf_xlatetom(elf, &dst, &src)) { 83 | sd->sd_memdata = (char*)dst.d_buf; 84 | sd->sd_data = dst; 85 | if (!(sd->sd_free_data = flag)) { 86 | elf->e_cooked = 1; 87 | } 88 | return &sd->sd_data; 89 | } 90 | 91 | if (flag) { 92 | free(dst.d_buf); 93 | } 94 | return NULL; 95 | } 96 | 97 | Elf_Data* 98 | elf_getdata(Elf_Scn *scn, Elf_Data *data) { 99 | Scn_Data *sd; 100 | Elf *elf; 101 | 102 | if (!scn) { 103 | return NULL; 104 | } 105 | elf_assert(scn->s_magic == SCN_MAGIC); 106 | if (scn->s_index == SHN_UNDEF) { 107 | seterr(ERROR_NULLSCN); 108 | } 109 | else if (data) { 110 | for (sd = scn->s_data_1; sd; sd = sd->sd_link) { 111 | elf_assert(sd->sd_magic == DATA_MAGIC); 112 | elf_assert(sd->sd_scn == scn); 113 | if (data == &sd->sd_data) { 114 | /* 115 | * sd_link allocated by elf_newdata(). 116 | */ 117 | return &sd->sd_link->sd_data; 118 | } 119 | } 120 | seterr(ERROR_SCNDATAMISMATCH); 121 | } 122 | else if ((sd = scn->s_data_1)) { 123 | elf_assert(sd->sd_magic == DATA_MAGIC); 124 | elf_assert(sd->sd_scn == scn); 125 | elf = scn->s_elf; 126 | elf_assert(elf); 127 | elf_assert(elf->e_magic == ELF_MAGIC); 128 | if (sd->sd_freeme) { 129 | /* allocated by elf_newdata() */ 130 | return &sd->sd_data; 131 | } 132 | else if (scn->s_type == SHT_NULL) { 133 | seterr(ERROR_NULLSCN); 134 | } 135 | else if (sd->sd_memdata) { 136 | /* already cooked */ 137 | return &sd->sd_data; 138 | } 139 | else if (scn->s_offset < 0 || scn->s_offset > elf->e_size) { 140 | seterr(ERROR_OUTSIDE); 141 | } 142 | else if (scn->s_type == SHT_NOBITS || !scn->s_size) { 143 | /* no data to read */ 144 | return &sd->sd_data; 145 | } 146 | else if (scn->s_offset + scn->s_size > elf->e_size) { 147 | seterr(ERROR_TRUNC_SCN); 148 | } 149 | else if (valid_class(elf->e_class)) { 150 | return _elf_cook_scn(elf, scn, sd); 151 | } 152 | else { 153 | seterr(ERROR_UNKNOWN_CLASS); 154 | } 155 | } 156 | return NULL; 157 | } 158 | -------------------------------------------------------------------------------- /lib/getident.c: -------------------------------------------------------------------------------- 1 | /* 2 | getident.c - implementation of the elf_getident(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: getident.c,v 1.7 2008/05/23 08:15:34 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | char* 27 | elf_getident(Elf *elf, size_t *ptr) { 28 | size_t tmp; 29 | 30 | if (!ptr) { 31 | ptr = &tmp; 32 | } 33 | if (!elf) { 34 | *ptr = 0; 35 | return NULL; 36 | } 37 | elf_assert(elf->e_magic == ELF_MAGIC); 38 | if (elf->e_kind != ELF_K_ELF) { 39 | *ptr = elf->e_idlen; 40 | return elf->e_data; 41 | } 42 | if (elf->e_ehdr || _elf_cook(elf)) { 43 | *ptr = elf->e_idlen; 44 | return elf->e_ehdr; 45 | } 46 | *ptr = 0; 47 | return NULL; 48 | } 49 | -------------------------------------------------------------------------------- /lib/getscn.c: -------------------------------------------------------------------------------- 1 | /* 2 | getscn.c - implementation of the elf_getscn(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: getscn.c,v 1.7 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | Elf_Scn* 27 | elf_getscn(Elf *elf, size_t index) { 28 | Elf_Scn *scn; 29 | 30 | if (!elf) { 31 | return NULL; 32 | } 33 | elf_assert(elf->e_magic == ELF_MAGIC); 34 | if (elf->e_kind != ELF_K_ELF) { 35 | seterr(ERROR_NOTELF); 36 | } 37 | else if (elf->e_ehdr || _elf_cook(elf)) { 38 | for (scn = elf->e_scn_1; scn; scn = scn->s_link) { 39 | elf_assert(scn->s_magic == SCN_MAGIC); 40 | elf_assert(scn->s_elf == elf); 41 | if (scn->s_index == index) { 42 | return scn; 43 | } 44 | } 45 | seterr(ERROR_NOSUCHSCN); 46 | } 47 | return NULL; 48 | } 49 | -------------------------------------------------------------------------------- /lib/hash.c: -------------------------------------------------------------------------------- 1 | /* 2 | hash.c - implementation of the elf_hash(3) function. 3 | Copyright (C) 1995 - 2002 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: hash.c,v 1.10 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | unsigned long 27 | elf_hash(const unsigned char *name) { 28 | unsigned long hash = 0; 29 | unsigned long tmp; 30 | 31 | while (*name) { 32 | hash = (hash << 4) + (unsigned char)*name++; 33 | if ((tmp = hash & 0xf0000000)) { 34 | hash ^= tmp | (tmp >> 24); 35 | } 36 | } 37 | return hash; 38 | } 39 | -------------------------------------------------------------------------------- /lib/input.c: -------------------------------------------------------------------------------- 1 | /* 2 | * input.c - low-level input for libelf. 3 | * Copyright (C) 1995 - 2001, 2005 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: input.c,v 1.11 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | #include 27 | 28 | #if HAVE_MMAP 29 | #include 30 | #endif /* HAVE_MMAP */ 31 | 32 | static int 33 | xread(int fd, char *buffer, size_t len) { 34 | size_t done = 0; 35 | size_t n; 36 | 37 | while (done < len) { 38 | n = read(fd, buffer + done, len - done); 39 | if (n == 0) { 40 | /* premature end of file */ 41 | return -1; 42 | } 43 | else if (n != (size_t)-1) { 44 | /* some bytes read, continue */ 45 | done += n; 46 | } 47 | else if (errno != EAGAIN && errno != EINTR) { 48 | /* real error */ 49 | return -1; 50 | } 51 | } 52 | return 0; 53 | } 54 | 55 | void* 56 | _elf_read(Elf *elf, void *buffer, size_t off, size_t len) { 57 | void *tmp; 58 | 59 | elf_assert(elf); 60 | elf_assert(elf->e_magic == ELF_MAGIC); 61 | elf_assert(off >= 0 && off + len <= elf->e_size); 62 | if (elf->e_disabled) { 63 | seterr(ERROR_FDDISABLED); 64 | } 65 | else if (len) { 66 | off += elf->e_base; 67 | if (lseek(elf->e_fd, (off_t)off, SEEK_SET) != (off_t)off) { 68 | seterr(ERROR_IO_SEEK); 69 | } 70 | else if (!(tmp = buffer) && !(tmp = malloc(len))) { 71 | seterr(ERROR_IO_2BIG); 72 | } 73 | else if (xread(elf->e_fd, tmp, len)) { 74 | seterr(ERROR_IO_READ); 75 | if (tmp != buffer) { 76 | free(tmp); 77 | } 78 | } 79 | else { 80 | return tmp; 81 | } 82 | } 83 | return NULL; 84 | } 85 | 86 | void* 87 | _elf_mmap(Elf *elf) { 88 | #if HAVE_MMAP 89 | void *tmp; 90 | 91 | elf_assert(elf); 92 | elf_assert(elf->e_magic == ELF_MAGIC); 93 | elf_assert(elf->e_base == 0); 94 | if (elf->e_disabled) { 95 | seterr(ERROR_FDDISABLED); 96 | } 97 | else if (elf->e_size) { 98 | tmp = (void*)mmap(0, elf->e_size, PROT_READ | PROT_WRITE, 99 | MAP_PRIVATE, elf->e_fd, 0); 100 | if (tmp != (void*)-1) { 101 | return tmp; 102 | } 103 | } 104 | #endif /* HAVE_MMAP */ 105 | return NULL; 106 | } 107 | -------------------------------------------------------------------------------- /lib/kind.c: -------------------------------------------------------------------------------- 1 | /* 2 | kind.c - implementation of the elf_kind(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: kind.c,v 1.7 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | Elf_Kind 27 | elf_kind(Elf *elf) { 28 | if (!elf) { 29 | return ELF_K_NONE; 30 | } 31 | elf_assert(elf->e_magic == ELF_MAGIC); 32 | return elf->e_kind; 33 | } 34 | -------------------------------------------------------------------------------- /lib/libelf.def: -------------------------------------------------------------------------------- 1 | LIBRARY libelf 2 | VERSION 0.8 3 | EXPORTS 4 | elf_begin 5 | elf_cntl 6 | elf_delscn 7 | elf_end 8 | elf_errmsg 9 | elf_errno 10 | elf_fill 11 | elf_flagdata 12 | elf_flagehdr 13 | elf_flagelf 14 | elf_flagphdr 15 | elf_flagscn 16 | elf_flagshdr 17 | elf_getarhdr 18 | elf_getarsym 19 | elf_getbase 20 | elf_getdata 21 | elf_getident 22 | elf_getscn 23 | elf_hash 24 | elf_kind 25 | elf_memory 26 | elf_ndxscn 27 | elf_newdata 28 | elf_newscn 29 | elf_next 30 | elf_nextscn 31 | elf_rand 32 | elf_rawdata 33 | elf_rawfile 34 | elf_strptr 35 | elf_update 36 | elf_version 37 | elf32_checksum 38 | elf32_fsize 39 | elf32_getehdr 40 | elf32_getphdr 41 | elf32_getshdr 42 | elf32_newehdr 43 | elf32_newphdr 44 | elf32_xlatetof 45 | elf32_xlatetom 46 | elf64_checksum 47 | elf64_fsize 48 | elf64_getehdr 49 | elf64_getphdr 50 | elf64_getshdr 51 | elf64_newehdr 52 | elf64_newphdr 53 | elf64_xlatetof 54 | elf64_xlatetom 55 | elfx_movscn 56 | elfx_remscn 57 | gelf_checksum 58 | gelf_fsize 59 | gelf_getclass 60 | gelf_getdyn 61 | gelf_getehdr 62 | gelf_getphdr 63 | gelf_getrel 64 | gelf_getrela 65 | gelf_getshdr 66 | gelf_getsym 67 | gelf_msize 68 | gelf_newehdr 69 | gelf_newphdr 70 | gelf_update_dyn 71 | gelf_update_ehdr 72 | gelf_update_phdr 73 | gelf_update_rel 74 | gelf_update_rela 75 | gelf_update_shdr 76 | gelf_update_sym 77 | gelf_xlatetof 78 | gelf_xlatetom 79 | elf_getphnum 80 | elf_getshnum 81 | elf_getshstrndx 82 | elfx_update_shstrndx 83 | -------------------------------------------------------------------------------- /lib/memset.c: -------------------------------------------------------------------------------- 1 | /* 2 | * memset.c - replacement for memset(3), using duff's device. 3 | * Copyright (C) 1995 - 2004 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #if HAVE_CONFIG_H 21 | # include 22 | #endif /* HAVE_CONFIG_H */ 23 | 24 | #ifndef lint 25 | static const char rcsid[] = "@(#) $Id: memset.c,v 1.11 2008/05/23 08:15:35 michael Exp $"; 26 | #endif /* lint */ 27 | 28 | #include /* for size_t */ 29 | #include 30 | 31 | void* 32 | _elf_memset(void *s, int c, size_t n) { 33 | char *t = (char*)s; 34 | 35 | if (n) { 36 | switch (n % 8u) { 37 | do { 38 | n -= 8; 39 | default: 40 | case 0: *t++ = (char)c; 41 | case 7: *t++ = (char)c; 42 | case 6: *t++ = (char)c; 43 | case 5: *t++ = (char)c; 44 | case 4: *t++ = (char)c; 45 | case 3: *t++ = (char)c; 46 | case 2: *t++ = (char)c; 47 | case 1: *t++ = (char)c; 48 | } 49 | while (n > 8); 50 | } 51 | } 52 | return s; 53 | } 54 | -------------------------------------------------------------------------------- /lib/ndxscn.c: -------------------------------------------------------------------------------- 1 | /* 2 | ndxscn.c - implementation of the elf_ndxscn(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: ndxscn.c,v 1.7 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | size_t 27 | elf_ndxscn(Elf_Scn *scn) { 28 | if (!scn) { 29 | return SHN_UNDEF; 30 | } 31 | elf_assert(scn->s_magic == SCN_MAGIC); 32 | return scn->s_index; 33 | } 34 | -------------------------------------------------------------------------------- /lib/newdata.c: -------------------------------------------------------------------------------- 1 | /* 2 | newdata.c - implementation of the elf_newdata(3) function. 3 | Copyright (C) 1995 - 2000 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: newdata.c,v 1.10 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | Elf_Data* 27 | elf_newdata(Elf_Scn *scn) { 28 | Scn_Data *sd; 29 | 30 | if (!scn) { 31 | return NULL; 32 | } 33 | elf_assert(scn->s_magic == SCN_MAGIC); 34 | if (scn->s_index == SHN_UNDEF) { 35 | seterr(ERROR_NULLSCN); 36 | } 37 | else if (!(sd = (Scn_Data*)malloc(sizeof(*sd)))) { 38 | seterr(ERROR_MEM_SCNDATA); 39 | } 40 | else { 41 | *sd = _elf_data_init; 42 | sd->sd_scn = scn; 43 | sd->sd_data_flags = ELF_F_DIRTY; 44 | sd->sd_freeme = 1; 45 | sd->sd_data.d_version = _elf_version; 46 | if (scn->s_data_n) { 47 | scn->s_data_n->sd_link = sd; 48 | } 49 | else { 50 | scn->s_data_1 = sd; 51 | } 52 | scn->s_data_n = sd; 53 | return &sd->sd_data; 54 | } 55 | return NULL; 56 | } 57 | -------------------------------------------------------------------------------- /lib/newscn.c: -------------------------------------------------------------------------------- 1 | /* 2 | * newscn.c - implementation of the elf_newscn(3) function. 3 | * Copyright (C) 1995 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: newscn.c,v 1.13 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | int 27 | _elf_update_shnum(Elf *elf, size_t shnum) { 28 | size_t extshnum = 0; 29 | Elf_Scn *scn; 30 | 31 | elf_assert(elf); 32 | elf_assert(elf->e_ehdr); 33 | scn = elf->e_scn_1; 34 | elf_assert(scn); 35 | elf_assert(scn->s_index == 0); 36 | if (shnum >= SHN_LORESERVE) { 37 | extshnum = shnum; 38 | shnum = 0; 39 | } 40 | if (elf->e_class == ELFCLASS32) { 41 | ((Elf32_Ehdr*)elf->e_ehdr)->e_shnum = shnum; 42 | scn->s_shdr32.sh_size = extshnum; 43 | } 44 | #if __LIBELF64 45 | else if (elf->e_class == ELFCLASS64) { 46 | ((Elf64_Ehdr*)elf->e_ehdr)->e_shnum = shnum; 47 | scn->s_shdr64.sh_size = extshnum; 48 | } 49 | #endif /* __LIBELF64 */ 50 | else { 51 | if (valid_class(elf->e_class)) { 52 | seterr(ERROR_UNIMPLEMENTED); 53 | } 54 | else { 55 | seterr(ERROR_UNKNOWN_CLASS); 56 | } 57 | return -1; 58 | } 59 | elf->e_ehdr_flags |= ELF_F_DIRTY; 60 | scn->s_shdr_flags |= ELF_F_DIRTY; 61 | return 0; 62 | } 63 | 64 | static Elf_Scn* 65 | _makescn(Elf *elf, size_t index) { 66 | Elf_Scn *scn; 67 | 68 | elf_assert(elf); 69 | elf_assert(elf->e_magic == ELF_MAGIC); 70 | elf_assert(elf->e_ehdr); 71 | elf_assert(_elf_scn_init.s_magic == SCN_MAGIC); 72 | if (!(scn = (Elf_Scn*)malloc(sizeof(*scn)))) { 73 | seterr(ERROR_MEM_SCN); 74 | return NULL; 75 | } 76 | *scn = _elf_scn_init; 77 | scn->s_elf = elf; 78 | scn->s_scn_flags = ELF_F_DIRTY; 79 | scn->s_shdr_flags = ELF_F_DIRTY; 80 | scn->s_freeme = 1; 81 | scn->s_index = index; 82 | return scn; 83 | } 84 | 85 | Elf_Scn* 86 | _elf_first_scn(Elf *elf) { 87 | Elf_Scn *scn; 88 | 89 | elf_assert(elf); 90 | elf_assert(elf->e_magic == ELF_MAGIC); 91 | if ((scn = elf->e_scn_1)) { 92 | return scn; 93 | } 94 | if ((scn = _makescn(elf, 0))) { 95 | elf->e_scn_1 = elf->e_scn_n = scn; 96 | if (_elf_update_shnum(elf, 1)) { 97 | free(scn); 98 | elf->e_scn_1 = elf->e_scn_n = scn = NULL; 99 | } 100 | } 101 | return scn; 102 | } 103 | 104 | static Elf_Scn* 105 | _buildscn(Elf *elf) { 106 | Elf_Scn *scn; 107 | 108 | if (!_elf_first_scn(elf)) { 109 | return NULL; 110 | } 111 | scn = elf->e_scn_n; 112 | elf_assert(scn); 113 | if (!(scn = _makescn(elf, scn->s_index + 1))) { 114 | return NULL; 115 | } 116 | if (_elf_update_shnum(elf, scn->s_index + 1)) { 117 | free(scn); 118 | return NULL; 119 | } 120 | elf->e_scn_n = elf->e_scn_n->s_link = scn; 121 | return scn; 122 | } 123 | 124 | Elf_Scn* 125 | elf_newscn(Elf *elf) { 126 | Elf_Scn *scn; 127 | 128 | if (!elf) { 129 | return NULL; 130 | } 131 | elf_assert(elf->e_magic == ELF_MAGIC); 132 | if (!elf->e_readable && !elf->e_ehdr) { 133 | seterr(ERROR_NOEHDR); 134 | } 135 | else if (elf->e_kind != ELF_K_ELF) { 136 | seterr(ERROR_NOTELF); 137 | } 138 | else if (!elf->e_ehdr && !_elf_cook(elf)) { 139 | return NULL; 140 | } 141 | else if ((scn = _buildscn(elf))) { 142 | return scn; 143 | } 144 | return NULL; 145 | } 146 | -------------------------------------------------------------------------------- /lib/next.c: -------------------------------------------------------------------------------- 1 | /* 2 | next.c - implementation of the elf_next(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: next.c,v 1.7 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | Elf_Cmd 27 | elf_next(Elf *elf) { 28 | if (!elf) { 29 | return ELF_C_NULL; 30 | } 31 | elf_assert(elf->e_magic == ELF_MAGIC); 32 | if (!elf->e_parent) { 33 | return ELF_C_NULL; 34 | } 35 | elf_assert(elf->e_parent->e_magic == ELF_MAGIC); 36 | elf_assert(elf->e_parent->e_kind == ELF_K_AR); 37 | elf->e_parent->e_off = elf->e_next; 38 | if (elf->e_next == elf->e_parent->e_size) { 39 | return ELF_C_NULL; 40 | } 41 | return ELF_C_READ; 42 | } 43 | -------------------------------------------------------------------------------- /lib/nextscn.c: -------------------------------------------------------------------------------- 1 | /* 2 | nextscn.c - implementation of the elf_nextscn(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: nextscn.c,v 1.7 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | Elf_Scn* 27 | elf_nextscn(Elf *elf, Elf_Scn *scn) { 28 | if (!elf) { 29 | return NULL; 30 | } 31 | elf_assert(elf->e_magic == ELF_MAGIC); 32 | if (scn) { 33 | elf_assert(scn->s_magic == SCN_MAGIC); 34 | if (scn->s_elf == elf) { 35 | return scn->s_link; 36 | } 37 | seterr(ERROR_ELFSCNMISMATCH); 38 | } 39 | else if (elf->e_kind != ELF_K_ELF) { 40 | seterr(ERROR_NOTELF); 41 | } 42 | else if (elf->e_ehdr || _elf_cook(elf)) { 43 | elf_assert(elf->e_ehdr); 44 | for (scn = elf->e_scn_1; scn; scn = scn->s_link) { 45 | elf_assert(scn->s_magic == SCN_MAGIC); 46 | elf_assert(scn->s_elf == elf); 47 | if (scn->s_index == 1) { 48 | return scn; 49 | } 50 | } 51 | seterr(ERROR_NOSUCHSCN); 52 | } 53 | return NULL; 54 | } 55 | -------------------------------------------------------------------------------- /lib/nlist.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nlist.c - implementation of the nlist(3) function. 3 | * Copyright (C) 1995 - 2004 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #ifndef lint 24 | static const char rcsid[] = "@(#) $Id: nlist.c,v 1.15 2008/05/23 08:15:35 michael Exp $"; 25 | #endif /* lint */ 26 | 27 | #if !defined(_WIN32) 28 | #if HAVE_FCNTL_H 29 | #include 30 | #else 31 | extern int open(); 32 | #endif /* HAVE_FCNTL_H */ 33 | #endif /* defined(_WIN32) */ 34 | 35 | #ifndef O_RDONLY 36 | #define O_RDONLY 0 37 | #endif /* O_RDONLY */ 38 | 39 | #ifndef O_BINARY 40 | #define O_BINARY 0 41 | #endif /* O_BINARY */ 42 | 43 | #define FILE_OPEN_MODE (O_RDONLY | O_BINARY) 44 | 45 | #define PRIME 217 46 | 47 | struct hash { 48 | const char* name; 49 | unsigned long hash; 50 | unsigned next; 51 | }; 52 | 53 | static const char* 54 | symbol_name(Elf *elf, const void *syms, const char *names, size_t nlimit, size_t index) { 55 | size_t off; 56 | 57 | if (elf->e_class == ELFCLASS32) { 58 | off = ((Elf32_Sym*)syms)[index].st_name; 59 | } 60 | #if __LIBELF64 61 | else if (elf->e_class == ELFCLASS64) { 62 | off = ((Elf64_Sym*)syms)[index].st_name; 63 | } 64 | #endif /* __LIBELF64 */ 65 | else { 66 | return NULL; 67 | } 68 | if (off >= 0 && off < nlimit) { 69 | return &names[off]; 70 | } 71 | return NULL; 72 | } 73 | 74 | static void 75 | copy_symbol(Elf *elf, struct nlist *np, const void *syms, size_t index) { 76 | if (elf->e_class == ELFCLASS32) { 77 | np->n_value = ((Elf32_Sym*)syms)[index].st_value; 78 | np->n_scnum = ((Elf32_Sym*)syms)[index].st_shndx; 79 | } 80 | #if __LIBELF64 81 | else if (elf->e_class == ELFCLASS64) { 82 | np->n_value = ((Elf64_Sym*)syms)[index].st_value; 83 | np->n_scnum = ((Elf64_Sym*)syms)[index].st_shndx; 84 | } 85 | #endif /* __LIBELF64 */ 86 | /* 87 | * this needs more work 88 | */ 89 | np->n_type = 0; 90 | np->n_sclass = 0; 91 | np->n_numaux = 0; 92 | } 93 | 94 | static int 95 | _elf_nlist(Elf *elf, struct nlist *nl) { 96 | unsigned first[PRIME]; 97 | Elf_Scn *symtab = NULL; 98 | Elf_Scn *strtab = NULL; 99 | Elf_Data *symdata; 100 | Elf_Data *strdata; 101 | size_t symsize; 102 | size_t nsymbols; 103 | const char *name; 104 | struct hash *table; 105 | unsigned long hash; 106 | unsigned i; 107 | struct nlist *np; 108 | 109 | /* 110 | * Get and translate ELF header, section table and so on. 111 | * Must be class independent, so don't use elf32_get*(). 112 | */ 113 | if (elf->e_kind != ELF_K_ELF) { 114 | return -1; 115 | } 116 | if (!elf->e_ehdr && !_elf_cook(elf)) { 117 | return -1; 118 | } 119 | 120 | /* 121 | * Find symbol table. If there is none, try dynamic symbols. 122 | */ 123 | for (symtab = elf->e_scn_1; symtab; symtab = symtab->s_link) { 124 | if (symtab->s_type == SHT_SYMTAB) { 125 | break; 126 | } 127 | if (symtab->s_type == SHT_DYNSYM) { 128 | strtab = symtab; 129 | } 130 | } 131 | if (!symtab && !(symtab = strtab)) { 132 | return -1; 133 | } 134 | 135 | /* 136 | * Get associated string table. 137 | */ 138 | i = 0; 139 | if (elf->e_class == ELFCLASS32) { 140 | i = symtab->s_shdr32.sh_link; 141 | } 142 | #if __LIBELF64 143 | else if (elf->e_class == ELFCLASS64) { 144 | i = symtab->s_shdr64.sh_link; 145 | } 146 | #endif /* __LIBELF64 */ 147 | if (i == 0) { 148 | return -1; 149 | } 150 | for (strtab = elf->e_scn_1; strtab; strtab = strtab->s_link) { 151 | if (strtab->s_index == i) { 152 | break; 153 | } 154 | } 155 | if (!strtab || strtab->s_type != SHT_STRTAB) { 156 | return -1; 157 | } 158 | 159 | /* 160 | * Get and translate section data. 161 | */ 162 | symdata = elf_getdata(symtab, NULL); 163 | strdata = elf_getdata(strtab, NULL); 164 | if (!symdata || !strdata) { 165 | return -1; 166 | } 167 | symsize = _msize(elf->e_class, _elf_version, ELF_T_SYM); 168 | elf_assert(symsize); 169 | nsymbols = symdata->d_size / symsize; 170 | if (!symdata->d_buf || !strdata->d_buf || !nsymbols || !strdata->d_size) { 171 | return -1; 172 | } 173 | 174 | /* 175 | * Build a simple hash table. 176 | */ 177 | if (!(table = (struct hash*)malloc(nsymbols * sizeof(*table)))) { 178 | return -1; 179 | } 180 | for (i = 0; i < PRIME; i++) { 181 | first[i] = 0; 182 | } 183 | for (i = 0; i < nsymbols; i++) { 184 | table[i].name = NULL; 185 | table[i].hash = 0; 186 | table[i].next = 0; 187 | } 188 | for (i = 1; i < nsymbols; i++) { 189 | name = symbol_name(elf, symdata->d_buf, strdata->d_buf, 190 | strdata->d_size, i); 191 | if (name == NULL) { 192 | free(table); 193 | return -1; 194 | } 195 | if (*name != '\0') { 196 | table[i].name = name; 197 | table[i].hash = elf_hash((unsigned char*)name); 198 | hash = table[i].hash % PRIME; 199 | table[i].next = first[hash]; 200 | first[hash] = i; 201 | } 202 | } 203 | 204 | /* 205 | * Lookup symbols, one by one. 206 | */ 207 | for (np = nl; (name = np->n_name) && *name; np++) { 208 | hash = elf_hash((unsigned char*)name); 209 | for (i = first[hash % PRIME]; i; i = table[i].next) { 210 | if (table[i].hash == hash && !strcmp(table[i].name, name)) { 211 | break; 212 | } 213 | } 214 | if (i) { 215 | copy_symbol(elf, np, symdata->d_buf, i); 216 | } 217 | else { 218 | np->n_value = 0; 219 | np->n_scnum = 0; 220 | np->n_type = 0; 221 | np->n_sclass = 0; 222 | np->n_numaux = 0; 223 | } 224 | } 225 | free(table); 226 | return 0; 227 | } 228 | 229 | int 230 | nlist(const char *filename, struct nlist *nl) { 231 | int result = -1; 232 | unsigned oldver; 233 | Elf *elf; 234 | int fd; 235 | 236 | if ((oldver = elf_version(EV_CURRENT)) != EV_NONE) { 237 | if ((fd = open(filename, FILE_OPEN_MODE)) != -1) { 238 | if ((elf = elf_begin(fd, ELF_C_READ, NULL))) { 239 | result = _elf_nlist(elf, nl); 240 | elf_end(elf); 241 | } 242 | close(fd); 243 | } 244 | elf_version(oldver); 245 | } 246 | if (result) { 247 | while (nl->n_name && *nl->n_name) { 248 | nl->n_value = 0; 249 | nl++; 250 | } 251 | } 252 | return result; 253 | } 254 | -------------------------------------------------------------------------------- /lib/nlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * nlist.h - public header file for nlist(3). 3 | * Copyright (C) 1995 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | /* @(#) $Id: nlist.h,v 1.10 2008/05/23 08:15:35 michael Exp $ */ 21 | 22 | #ifndef _NLIST_H 23 | #define _NLIST_H 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif /* __cplusplus */ 28 | 29 | struct nlist { 30 | char* n_name; 31 | long n_value; 32 | short n_scnum; 33 | unsigned short n_type; 34 | char n_sclass; 35 | char n_numaux; 36 | }; 37 | 38 | #if (__STDC__ + 0) || defined(__cplusplus) || defined(_WIN32) 39 | extern int nlist(const char *__filename, struct nlist *__nl); 40 | #else /* __STDC__ || defined(__cplusplus) */ 41 | extern int nlist(); 42 | #endif /* __STDC__ || defined(__cplusplus) */ 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif /* __cplusplus */ 47 | 48 | #endif /* _NLIST_H */ 49 | -------------------------------------------------------------------------------- /lib/opt.delscn.c: -------------------------------------------------------------------------------- 1 | /* 2 | opt.delscn.c - implementation of the elf_delscn(3) function. 3 | Copyright (C) 1995 - 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: opt.delscn.c,v 1.12 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | static size_t 27 | _newindex(size_t old, size_t index) { 28 | return old == index ? SHN_UNDEF : (old > index ? old - 1 : old); 29 | } 30 | 31 | static void 32 | _elf32_update_shdr(Elf *elf, size_t index) { 33 | Elf32_Shdr *shdr; 34 | Elf_Scn *scn; 35 | 36 | ((Elf32_Ehdr*)elf->e_ehdr)->e_shnum = elf->e_scn_n->s_index + 1; 37 | for (scn = elf->e_scn_1; scn; scn = scn->s_link) { 38 | shdr = &scn->s_shdr32; 39 | switch (shdr->sh_type) { 40 | case SHT_REL: 41 | case SHT_RELA: 42 | shdr->sh_info = _newindex(shdr->sh_info, index); 43 | /* fall through */ 44 | case SHT_DYNSYM: 45 | case SHT_DYNAMIC: 46 | case SHT_HASH: 47 | case SHT_SYMTAB: 48 | #if __LIBELF_SYMBOL_VERSIONS 49 | #if __LIBELF_SUN_SYMBOL_VERSIONS 50 | case SHT_SUNW_verdef: 51 | case SHT_SUNW_verneed: 52 | case SHT_SUNW_versym: 53 | #else /* __LIBELF_SUN_SYMBOL_VERSIONS */ 54 | case SHT_GNU_verdef: 55 | case SHT_GNU_verneed: 56 | case SHT_GNU_versym: 57 | #endif /* __LIBELF_SUN_SYMBOL_VERSIONS */ 58 | #endif /* __LIBELF_SYMBOL_VERSIONS */ 59 | shdr->sh_link = _newindex(shdr->sh_link, index); 60 | /* fall through */ 61 | default: 62 | break; 63 | } 64 | } 65 | } 66 | 67 | #if __LIBELF64 68 | 69 | static void 70 | _elf64_update_shdr(Elf *elf, size_t index) { 71 | Elf64_Shdr *shdr; 72 | Elf_Scn *scn; 73 | 74 | ((Elf64_Ehdr*)elf->e_ehdr)->e_shnum = elf->e_scn_n->s_index + 1; 75 | for (scn = elf->e_scn_1; scn; scn = scn->s_link) { 76 | shdr = &scn->s_shdr64; 77 | switch (shdr->sh_type) { 78 | case SHT_REL: 79 | case SHT_RELA: 80 | shdr->sh_info = _newindex(shdr->sh_info, index); 81 | /* fall through */ 82 | case SHT_DYNSYM: 83 | case SHT_DYNAMIC: 84 | case SHT_HASH: 85 | case SHT_SYMTAB: 86 | #if __LIBELF_SYMBOL_VERSIONS 87 | #if __LIBELF_SUN_SYMBOL_VERSIONS 88 | case SHT_SUNW_verdef: 89 | case SHT_SUNW_verneed: 90 | case SHT_SUNW_versym: 91 | #else /* __LIBELF_SUN_SYMBOL_VERSIONS */ 92 | case SHT_GNU_verdef: 93 | case SHT_GNU_verneed: 94 | case SHT_GNU_versym: 95 | #endif /* __LIBELF_SUN_SYMBOL_VERSIONS */ 96 | #endif /* __LIBELF_SYMBOL_VERSIONS */ 97 | shdr->sh_link = _newindex(shdr->sh_link, index); 98 | /* fall through */ 99 | default: 100 | break; 101 | } 102 | } 103 | } 104 | 105 | #endif /* __LIBELF64 */ 106 | 107 | size_t 108 | elf_delscn(Elf *elf, Elf_Scn *scn) { 109 | Elf_Scn *pscn; 110 | Scn_Data *sd; 111 | Scn_Data *tmp; 112 | size_t index; 113 | 114 | if (!elf || !scn) { 115 | return SHN_UNDEF; 116 | } 117 | elf_assert(elf->e_magic == ELF_MAGIC); 118 | elf_assert(scn->s_magic == SCN_MAGIC); 119 | elf_assert(elf->e_ehdr); 120 | if (scn->s_elf != elf) { 121 | seterr(ERROR_ELFSCNMISMATCH); 122 | return SHN_UNDEF; 123 | } 124 | elf_assert(elf->e_scn_1); 125 | if (scn == elf->e_scn_1) { 126 | seterr(ERROR_NULLSCN); 127 | return SHN_UNDEF; 128 | } 129 | 130 | /* 131 | * Find previous section. 132 | */ 133 | for (pscn = elf->e_scn_1; pscn->s_link; pscn = pscn->s_link) { 134 | if (pscn->s_link == scn) { 135 | break; 136 | } 137 | } 138 | if (pscn->s_link != scn) { 139 | seterr(ERROR_ELFSCNMISMATCH); 140 | return SHN_UNDEF; 141 | } 142 | /* 143 | * Unlink section. 144 | */ 145 | if (elf->e_scn_n == scn) { 146 | elf->e_scn_n = pscn; 147 | } 148 | pscn->s_link = scn->s_link; 149 | index = scn->s_index; 150 | /* 151 | * Free section descriptor and data. 152 | */ 153 | for (sd = scn->s_data_1; sd; sd = tmp) { 154 | elf_assert(sd->sd_magic == DATA_MAGIC); 155 | elf_assert(sd->sd_scn == scn); 156 | tmp = sd->sd_link; 157 | if (sd->sd_free_data && sd->sd_memdata) { 158 | free(sd->sd_memdata); 159 | } 160 | if (sd->sd_freeme) { 161 | free(sd); 162 | } 163 | } 164 | if ((sd = scn->s_rawdata)) { 165 | elf_assert(sd->sd_magic == DATA_MAGIC); 166 | elf_assert(sd->sd_scn == scn); 167 | if (sd->sd_free_data && sd->sd_memdata) { 168 | free(sd->sd_memdata); 169 | } 170 | if (sd->sd_freeme) { 171 | free(sd); 172 | } 173 | } 174 | if (scn->s_freeme) { 175 | elf_assert(scn->s_index > 0); 176 | free(scn); 177 | } 178 | /* 179 | * Adjust section indices. 180 | */ 181 | for (scn = pscn->s_link; scn; scn = scn->s_link) { 182 | elf_assert(scn->s_index > index); 183 | scn->s_index--; 184 | } 185 | /* 186 | * Adjust ELF header and well-known section headers. 187 | */ 188 | if (elf->e_class == ELFCLASS32) { 189 | _elf32_update_shdr(elf, index); 190 | return index; 191 | } 192 | #if __LIBELF64 193 | else if (elf->e_class == ELFCLASS64) { 194 | _elf64_update_shdr(elf, index); 195 | return index; 196 | } 197 | #endif /* __LIBELF64 */ 198 | else if (valid_class(elf->e_class)) { 199 | seterr(ERROR_UNIMPLEMENTED); 200 | } 201 | else { 202 | seterr(ERROR_UNKNOWN_CLASS); 203 | } 204 | return SHN_UNDEF; 205 | } 206 | -------------------------------------------------------------------------------- /lib/rand.c: -------------------------------------------------------------------------------- 1 | /* 2 | rand.c - implementation of the elf_rand(3) function. 3 | Copyright (C) 1995 - 1998 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: rand.c,v 1.7 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | size_t 27 | elf_rand(Elf *elf, size_t offset) { 28 | if (!elf) { 29 | return 0; 30 | } 31 | elf_assert(elf->e_magic == ELF_MAGIC); 32 | if (elf->e_kind != ELF_K_AR) { 33 | seterr(ERROR_NOTARCHIVE); 34 | } 35 | else if (offset <= 0 || offset > elf->e_size) { 36 | seterr(ERROR_BADOFF); 37 | } 38 | else { 39 | elf->e_off = offset; 40 | return offset; 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /lib/rawdata.c: -------------------------------------------------------------------------------- 1 | /* 2 | rawdata.c - implementation of the elf_rawdata(3) function. 3 | Copyright (C) 1995 - 2000 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: rawdata.c,v 1.10 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | Elf_Data* 27 | elf_rawdata(Elf_Scn *scn, Elf_Data *data) { 28 | Scn_Data *sd; 29 | Elf *elf; 30 | 31 | if (!scn) { 32 | return NULL; 33 | } 34 | elf_assert(scn->s_magic == SCN_MAGIC); 35 | elf = scn->s_elf; 36 | elf_assert(elf); 37 | elf_assert(elf->e_magic == ELF_MAGIC); 38 | if (!elf->e_readable) { 39 | return NULL; 40 | } 41 | else if (scn->s_index == SHN_UNDEF || scn->s_type == SHT_NULL) { 42 | seterr(ERROR_NULLSCN); 43 | } 44 | else if (data) { 45 | return NULL; 46 | } 47 | else if ((sd = scn->s_rawdata)) { 48 | elf_assert(sd->sd_magic == DATA_MAGIC); 49 | elf_assert(sd->sd_scn == scn); 50 | return &sd->sd_data; 51 | } 52 | else if (scn->s_offset < 0 || scn->s_offset > elf->e_size) { 53 | seterr(ERROR_OUTSIDE); 54 | } 55 | else if (scn->s_type != SHT_NOBITS 56 | && scn->s_offset + scn->s_size > elf->e_size) { 57 | seterr(ERROR_TRUNC_SCN); 58 | } 59 | else if (!(sd = (Scn_Data*)malloc(sizeof(*sd)))) { 60 | seterr(ERROR_MEM_SCNDATA); 61 | } 62 | else { 63 | *sd = _elf_data_init; 64 | sd->sd_scn = scn; 65 | sd->sd_freeme = 1; 66 | sd->sd_data.d_size = scn->s_size; 67 | sd->sd_data.d_version = _elf_version; 68 | if (scn->s_type != SHT_NOBITS && scn->s_size) { 69 | if (!(sd->sd_memdata = (char*)malloc(scn->s_size))) { 70 | seterr(ERROR_IO_2BIG); 71 | free(sd); 72 | return NULL; 73 | } 74 | else if (elf->e_rawdata) { 75 | memcpy(sd->sd_memdata, elf->e_rawdata + scn->s_offset, scn->s_size); 76 | } 77 | else if (!_elf_read(elf, sd->sd_memdata, scn->s_offset, scn->s_size)) { 78 | free(sd->sd_memdata); 79 | free(sd); 80 | return NULL; 81 | } 82 | sd->sd_data.d_buf = sd->sd_memdata; 83 | sd->sd_free_data = 1; 84 | } 85 | scn->s_rawdata = sd; 86 | return &sd->sd_data; 87 | } 88 | return NULL; 89 | } 90 | -------------------------------------------------------------------------------- /lib/rawfile.c: -------------------------------------------------------------------------------- 1 | /* 2 | * rawfile.c - implementation of the elf_rawfile(3) function. 3 | * Copyright (C) 1995 - 2009 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: rawfile.c,v 1.8 2009/05/22 17:07:46 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | char* 27 | elf_rawfile(Elf *elf, size_t *ptr) { 28 | size_t tmp; 29 | 30 | if (!ptr) { 31 | ptr = &tmp; 32 | } 33 | *ptr = 0; 34 | if (!elf) { 35 | return NULL; 36 | } 37 | elf_assert(elf->e_magic == ELF_MAGIC); 38 | if (!elf->e_readable) { 39 | return NULL; 40 | } 41 | else if (elf->e_size) { 42 | if (!elf->e_rawdata) { 43 | elf_assert(elf->e_data); 44 | if (!elf->e_cooked) { 45 | elf->e_rawdata = elf->e_data; 46 | } 47 | else if (!(elf->e_rawdata = _elf_read(elf, NULL, 0, elf->e_size))) { 48 | return NULL; 49 | } 50 | } 51 | *ptr = elf->e_size; 52 | } 53 | return elf->e_rawdata; 54 | } 55 | -------------------------------------------------------------------------------- /lib/strptr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * strptr.c - implementation of the elf_strptr(3) function. 3 | * Copyright (C) 1995 - 2007 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: strptr.c,v 1.12 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | char* 27 | elf_strptr(Elf *elf, size_t section, size_t offset) { 28 | Elf_Data *data; 29 | Elf_Scn *scn; 30 | size_t n; 31 | char *s; 32 | 33 | if (!elf) { 34 | return NULL; 35 | } 36 | elf_assert(elf->e_magic == ELF_MAGIC); 37 | if (!(scn = elf_getscn(elf, section))) { 38 | return NULL; 39 | } 40 | if (scn->s_index == SHN_UNDEF) { 41 | seterr(ERROR_NOSTRTAB); 42 | return NULL; 43 | } 44 | /* 45 | * checking the section header is more appropriate 46 | */ 47 | if (elf->e_class == ELFCLASS32) { 48 | if (scn->s_shdr32.sh_type != SHT_STRTAB) { 49 | seterr(ERROR_NOSTRTAB); 50 | return NULL; 51 | } 52 | } 53 | #if __LIBELF64 54 | else if (elf->e_class == ELFCLASS64) { 55 | if (scn->s_shdr64.sh_type != SHT_STRTAB) { 56 | seterr(ERROR_NOSTRTAB); 57 | return NULL; 58 | } 59 | } 60 | #endif /* __LIBELF64 */ 61 | else if (valid_class(elf->e_class)) { 62 | seterr(ERROR_UNIMPLEMENTED); 63 | return NULL; 64 | } 65 | else { 66 | seterr(ERROR_UNKNOWN_CLASS); 67 | return NULL; 68 | } 69 | /* 70 | * Find matching buffer 71 | */ 72 | n = 0; 73 | data = NULL; 74 | if (elf->e_elf_flags & ELF_F_LAYOUT) { 75 | /* 76 | * Programmer is responsible for d_off 77 | * Note: buffers may be in any order! 78 | */ 79 | while ((data = elf_getdata(scn, data))) { 80 | n = data->d_off; 81 | if (offset >= n && offset - n < data->d_size) { 82 | /* 83 | * Found it 84 | */ 85 | break; 86 | } 87 | } 88 | } 89 | else { 90 | /* 91 | * Calculate offsets myself 92 | */ 93 | while ((data = elf_getdata(scn, data))) { 94 | if (data->d_align > 1) { 95 | n += data->d_align - 1; 96 | n -= n % data->d_align; 97 | } 98 | if (offset < n) { 99 | /* 100 | * Invalid offset: points into a hole 101 | */ 102 | seterr(ERROR_BADSTROFF); 103 | return NULL; 104 | } 105 | if (offset - n < data->d_size) { 106 | /* 107 | * Found it 108 | */ 109 | break; 110 | } 111 | n += data->d_size; 112 | } 113 | } 114 | if (data == NULL) { 115 | /* 116 | * Not found 117 | */ 118 | seterr(ERROR_BADSTROFF); 119 | return NULL; 120 | } 121 | if (data->d_buf == NULL) { 122 | /* 123 | * Buffer is NULL (usually the programmers' fault) 124 | */ 125 | seterr(ERROR_NULLBUF); 126 | return NULL; 127 | } 128 | offset -= n; 129 | s = (char*)data->d_buf; 130 | if (!(_elf_sanity_checks & SANITY_CHECK_STRPTR)) { 131 | return s + offset; 132 | } 133 | /* 134 | * Perform extra sanity check 135 | */ 136 | for (n = offset; n < data->d_size; n++) { 137 | if (s[n] == '\0') { 138 | /* 139 | * Return properly NUL terminated string 140 | */ 141 | return s + offset; 142 | } 143 | } 144 | /* 145 | * String is not NUL terminated 146 | * Return error to avoid SEGV in application 147 | */ 148 | seterr(ERROR_UNTERM); 149 | return NULL; 150 | } 151 | -------------------------------------------------------------------------------- /lib/swap64.c: -------------------------------------------------------------------------------- 1 | /* 2 | swap64.c - 64-bit byte swapping functions. 3 | Copyright (C) 1995 - 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | #if __LIBELF64 24 | 25 | #ifndef lint 26 | static const char rcsid[] = "@(#) $Id: swap64.c,v 1.6 2008/05/23 08:15:35 michael Exp $"; 27 | #endif /* lint */ 28 | 29 | __libelf_u64_t 30 | _elf_load_u64L(const unsigned char *from) { 31 | return ((__libelf_u64_t)__load_u32L(from + 4) << 32) 32 | | (__libelf_u64_t)__load_u32L(from); 33 | } 34 | 35 | __libelf_u64_t 36 | _elf_load_u64M(const unsigned char *from) { 37 | return ((__libelf_u64_t)__load_u32M(from) << 32) 38 | | (__libelf_u64_t)__load_u32M(from + 4); 39 | } 40 | 41 | __libelf_i64_t 42 | _elf_load_i64L(const unsigned char *from) { 43 | return ((__libelf_i64_t)__load_i32L(from + 4) << 32) 44 | | (__libelf_u64_t)__load_u32L(from); 45 | } 46 | 47 | __libelf_i64_t 48 | _elf_load_i64M(const unsigned char *from) { 49 | return ((__libelf_i64_t)__load_i32M(from) << 32) 50 | | (__libelf_u64_t)__load_u32M(from + 4); 51 | } 52 | 53 | void 54 | _elf_store_u64L(unsigned char *to, __libelf_u64_t v) { 55 | __store_u32L(to, (__libelf_u32_t)v); 56 | v >>= 32; 57 | __store_u32L(to + 4, (__libelf_u32_t)v); 58 | } 59 | 60 | void 61 | _elf_store_u64M(unsigned char *to, __libelf_u64_t v) { 62 | __store_u32M(to + 4, (__libelf_u32_t)v); 63 | v >>= 32; 64 | __store_u32M(to, (__libelf_u32_t)v); 65 | } 66 | 67 | void 68 | _elf_store_i64L(unsigned char *to, __libelf_u64_t v) { 69 | __store_u32L(to, (__libelf_u32_t)v); 70 | v >>= 32; 71 | __store_i32L(to + 4, (__libelf_u32_t)v); 72 | } 73 | 74 | void 75 | _elf_store_i64M(unsigned char *to, __libelf_u64_t v) { 76 | __store_u32M(to + 4, (__libelf_u32_t)v); 77 | v >>= 32; 78 | __store_i32M(to, (__libelf_u32_t)v); 79 | } 80 | 81 | #endif /* __LIBELF64 */ 82 | -------------------------------------------------------------------------------- /lib/sys_elf.h.in: -------------------------------------------------------------------------------- 1 | /* 2 | sys_elf.h.in - configure template for private "switch" file. 3 | Copyright (C) 1998 - 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | /* @(#) $Id: sys_elf.h.in,v 1.13 2008/05/23 08:57:07 michael Exp $ */ 21 | 22 | /* 23 | * DO NOT USE THIS IN APPLICATIONS - #include INSTEAD! 24 | */ 25 | 26 | /* Define to `' or `' if one of them is present */ 27 | #undef __LIBELF_HEADER_ELF_H 28 | 29 | /* Define if Elf32_Dyn is declared in */ 30 | #undef __LIBELF_NEED_LINK_H 31 | 32 | /* Define if Elf32_Dyn is declared in */ 33 | #undef __LIBELF_NEED_SYS_LINK_H 34 | 35 | /* Define if you want 64-bit support (and your system supports it) */ 36 | #undef __LIBELF64 37 | 38 | /* Define if you want 64-bit support, and are running IRIX */ 39 | #undef __LIBELF64_IRIX 40 | 41 | /* Define if you want 64-bit support, and are running Linux */ 42 | #undef __LIBELF64_LINUX 43 | 44 | /* Define if you want symbol versioning (and your system supports it) */ 45 | #undef __LIBELF_SYMBOL_VERSIONS 46 | 47 | /* Define to a 64-bit signed integer type if one exists */ 48 | #undef __libelf_i64_t 49 | 50 | /* Define to a 64-bit unsigned integer type if one exists */ 51 | #undef __libelf_u64_t 52 | 53 | /* Define to a 32-bit signed integer type if one exists */ 54 | #undef __libelf_i32_t 55 | 56 | /* Define to a 32-bit unsigned integer type if one exists */ 57 | #undef __libelf_u32_t 58 | 59 | /* Define to a 16-bit signed integer type if one exists */ 60 | #undef __libelf_i16_t 61 | 62 | /* Define to a 16-bit unsigned integer type if one exists */ 63 | #undef __libelf_u16_t 64 | 65 | /* 66 | * Ok, now get the correct instance of elf.h... 67 | */ 68 | #ifdef __LIBELF_HEADER_ELF_H 69 | # include __LIBELF_HEADER_ELF_H 70 | #else /* __LIBELF_HEADER_ELF_H */ 71 | # if __LIBELF_INTERNAL__ 72 | # include 73 | # else /* __LIBELF_INTERNAL__ */ 74 | # include 75 | # endif /* __LIBELF_INTERNAL__ */ 76 | #endif /* __LIBELF_HEADER_ELF_H */ 77 | 78 | /* 79 | * On some systems, is severely broken. Try to fix it. 80 | */ 81 | #ifdef __LIBELF_HEADER_ELF_H 82 | 83 | # ifndef ELF32_FSZ_ADDR 84 | # define ELF32_FSZ_ADDR 4 85 | # define ELF32_FSZ_HALF 2 86 | # define ELF32_FSZ_OFF 4 87 | # define ELF32_FSZ_SWORD 4 88 | # define ELF32_FSZ_WORD 4 89 | # endif /* ELF32_FSZ_ADDR */ 90 | 91 | # ifndef STN_UNDEF 92 | # define STN_UNDEF 0 93 | # endif /* STN_UNDEF */ 94 | 95 | # if __LIBELF64 96 | 97 | # ifndef ELF64_FSZ_ADDR 98 | # define ELF64_FSZ_ADDR 8 99 | # define ELF64_FSZ_HALF 2 100 | # define ELF64_FSZ_OFF 8 101 | # define ELF64_FSZ_SWORD 4 102 | # define ELF64_FSZ_WORD 4 103 | # define ELF64_FSZ_SXWORD 8 104 | # define ELF64_FSZ_XWORD 8 105 | # endif /* ELF64_FSZ_ADDR */ 106 | 107 | # ifndef ELF64_ST_BIND 108 | # define ELF64_ST_BIND(i) ((i)>>4) 109 | # define ELF64_ST_TYPE(i) ((i)&0xf) 110 | # define ELF64_ST_INFO(b,t) (((b)<<4)+((t)&0xf)) 111 | # endif /* ELF64_ST_BIND */ 112 | 113 | # ifndef ELF64_R_SYM 114 | # define ELF64_R_SYM(i) ((Elf64_Xword)(i)>>32) 115 | # define ELF64_R_TYPE(i) ((i)&0xffffffffL) 116 | # define ELF64_R_INFO(s,t) (((Elf64_Xword)(s)<<32)+((t)&0xffffffffL)) 117 | # endif /* ELF64_R_SYM */ 118 | 119 | # if __LIBELF64_LINUX 120 | typedef __libelf_u64_t Elf64_Addr; 121 | typedef __libelf_u16_t Elf64_Half; 122 | typedef __libelf_u64_t Elf64_Off; 123 | typedef __libelf_i32_t Elf64_Sword; 124 | typedef __libelf_u32_t Elf64_Word; 125 | typedef __libelf_i64_t Elf64_Sxword; 126 | typedef __libelf_u64_t Elf64_Xword; 127 | # endif /* __LIBELF64_LINUX */ 128 | 129 | # endif /* __LIBELF64 */ 130 | #endif /* __LIBELF_HEADER_ELF_H */ 131 | -------------------------------------------------------------------------------- /lib/sys_elf.h.w32: -------------------------------------------------------------------------------- 1 | /* 2 | * lib/sys_elf.h.w32 - internal configuration file for W32 port 3 | * Copyright (C) 2004 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | * 19 | * @(#) $Id: sys_elf.h.w32,v 1.2 2006/09/07 15:55:42 michael Exp $ 20 | */ 21 | 22 | /* 23 | * DO NOT USE THIS IN APPLICATIONS - #include INSTEAD! 24 | */ 25 | 26 | /* Define to `' or `' if one of them is present */ 27 | #undef __LIBELF_HEADER_ELF_H 28 | 29 | /* Define if Elf32_Dyn is declared in */ 30 | #undef __LIBELF_NEED_LINK_H 31 | 32 | /* Define if Elf32_Dyn is declared in */ 33 | #undef __LIBELF_NEED_SYS_LINK_H 34 | 35 | /* Define if you want 64-bit support (and your system supports it) */ 36 | #define __LIBELF64 1 37 | 38 | /* Define if you want 64-bit support, and are running IRIX */ 39 | #undef __LIBELF64_IRIX 40 | 41 | /* Define if you want 64-bit support, and are running Linux */ 42 | #undef __LIBELF64_LINUX 43 | 44 | /* Define if you want symbol versioning (and your system supports it) */ 45 | #define __LIBELF_SYMBOL_VERSIONS 1 46 | 47 | /* Define to a 64-bit signed integer type if one exists */ 48 | #define __libelf_i64_t __int64 49 | 50 | /* Define to a 64-bit unsigned integer type if one exists */ 51 | #define __libelf_u64_t unsigned __int64 52 | 53 | /* Define to a 32-bit signed integer type if one exists */ 54 | #define __libelf_i32_t int 55 | 56 | /* Define to a 32-bit unsigned integer type if one exists */ 57 | #define __libelf_u32_t unsigned int 58 | 59 | /* Define to a 16-bit signed integer type if one exists */ 60 | #define __libelf_i16_t short int 61 | 62 | /* Define to a 16-bit unsigned integer type if one exists */ 63 | #define __libelf_u16_t unsigned short int 64 | 65 | /* 66 | * Ok, now get the correct instance of elf.h... 67 | */ 68 | #ifdef __LIBELF_HEADER_ELF_H 69 | # include __LIBELF_HEADER_ELF_H 70 | #else /* __LIBELF_HEADER_ELF_H */ 71 | # if __LIBELF_INTERNAL__ 72 | # include 73 | # else /* __LIBELF_INTERNAL__ */ 74 | # include 75 | # endif /* __LIBELF_INTERNAL__ */ 76 | #endif /* __LIBELF_HEADER_ELF_H */ 77 | 78 | /* 79 | * On some systems, is severely broken. Try to fix it. 80 | */ 81 | #ifdef __LIBELF_HEADER_ELF_H 82 | 83 | # ifndef ELF32_FSZ_ADDR 84 | # define ELF32_FSZ_ADDR 4 85 | # define ELF32_FSZ_HALF 2 86 | # define ELF32_FSZ_OFF 4 87 | # define ELF32_FSZ_SWORD 4 88 | # define ELF32_FSZ_WORD 4 89 | # endif /* ELF32_FSZ_ADDR */ 90 | 91 | # ifndef STN_UNDEF 92 | # define STN_UNDEF 0 93 | # endif /* STN_UNDEF */ 94 | 95 | # if __LIBELF64 96 | 97 | # ifndef ELF64_FSZ_ADDR 98 | # define ELF64_FSZ_ADDR 8 99 | # define ELF64_FSZ_HALF 2 100 | # define ELF64_FSZ_OFF 8 101 | # define ELF64_FSZ_SWORD 4 102 | # define ELF64_FSZ_WORD 4 103 | # define ELF64_FSZ_SXWORD 8 104 | # define ELF64_FSZ_XWORD 8 105 | # endif /* ELF64_FSZ_ADDR */ 106 | 107 | # ifndef ELF64_ST_BIND 108 | # define ELF64_ST_BIND(i) ((i)>>4) 109 | # define ELF64_ST_TYPE(i) ((i)&0xf) 110 | # define ELF64_ST_INFO(b,t) (((b)<<4)+((t)&0xf)) 111 | # endif /* ELF64_ST_BIND */ 112 | 113 | # ifndef ELF64_R_SYM 114 | # define ELF64_R_SYM(i) ((Elf64_Xword)(i)>>32) 115 | # define ELF64_R_TYPE(i) ((i)&0xffffffffL) 116 | # define ELF64_R_INFO(s,t) (((Elf64_Xword)(s)<<32)+((t)&0xffffffffL)) 117 | # endif /* ELF64_R_SYM */ 118 | 119 | # if __LIBELF64_LINUX 120 | typedef __libelf_u64_t Elf64_Addr; 121 | typedef __libelf_u16_t Elf64_Half; 122 | typedef __libelf_u64_t Elf64_Off; 123 | typedef __libelf_i32_t Elf64_Sword; 124 | typedef __libelf_u32_t Elf64_Word; 125 | typedef __libelf_i64_t Elf64_Sxword; 126 | typedef __libelf_u64_t Elf64_Xword; 127 | # endif /* __LIBELF64_LINUX */ 128 | 129 | # endif /* __LIBELF64 */ 130 | #endif /* __LIBELF_HEADER_ELF_H */ 131 | -------------------------------------------------------------------------------- /lib/verdef.h: -------------------------------------------------------------------------------- 1 | /* 2 | * verdef.h - copy versioning information. 3 | * Copyright (C) 2001 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #ifndef lint 21 | static const char verdef_h_rcsid[] = "@(#) $Id: verdef.h,v 1.13 2008/05/23 08:15:35 michael Exp $"; 22 | #endif /* lint */ 23 | 24 | #if VER_DEF_CURRENT != 1 25 | #error libelf currently does not support VER_DEF_CURRENT != 1 26 | #endif /* VER_DEF_CURRENT != 1 */ 27 | 28 | #if TOFILE 29 | 30 | static void 31 | __store_verdaux(verdaux_ftype *dst, const verdaux_mtype *src, unsigned enc) { 32 | if (enc == ELFDATA2LSB) { 33 | __store_u32L(dst->vda_name, src->vda_name); 34 | __store_u32L(dst->vda_next, src->vda_next); 35 | } 36 | else { 37 | __store_u32M(dst->vda_name, src->vda_name); 38 | __store_u32M(dst->vda_next, src->vda_next); 39 | } 40 | } 41 | 42 | static void 43 | __store_verdef(verdef_ftype *dst, const verdef_mtype *src, unsigned enc) { 44 | if (enc == ELFDATA2LSB) { 45 | __store_u16L(dst->vd_version, src->vd_version); 46 | __store_u16L(dst->vd_flags, src->vd_flags); 47 | __store_u16L(dst->vd_ndx, src->vd_ndx); 48 | __store_u16L(dst->vd_cnt, src->vd_cnt); 49 | __store_u32L(dst->vd_hash, src->vd_hash); 50 | __store_u32L(dst->vd_aux, src->vd_aux); 51 | __store_u32L(dst->vd_next, src->vd_next); 52 | } 53 | else { 54 | __store_u16M(dst->vd_version, src->vd_version); 55 | __store_u16M(dst->vd_flags, src->vd_flags); 56 | __store_u16M(dst->vd_ndx, src->vd_ndx); 57 | __store_u16M(dst->vd_cnt, src->vd_cnt); 58 | __store_u32M(dst->vd_hash, src->vd_hash); 59 | __store_u32M(dst->vd_aux, src->vd_aux); 60 | __store_u32M(dst->vd_next, src->vd_next); 61 | } 62 | } 63 | 64 | typedef verdaux_mtype verdaux_stype; 65 | typedef verdaux_ftype verdaux_dtype; 66 | typedef verdef_mtype verdef_stype; 67 | typedef verdef_ftype verdef_dtype; 68 | typedef align_mtype verdef_atype; 69 | 70 | #define copy_verdaux_srctotmp(d, s, e) (*(d) = *(s)) 71 | #define copy_verdaux_tmptodst(d, s, e) __store_verdaux((d), (s), (e)) 72 | #define copy_verdef_srctotmp(d, s, e) (*(d) = *(s)) 73 | #define copy_verdef_tmptodst(d, s, e) __store_verdef((d), (s), (e)) 74 | 75 | #define translator_suffix _tof 76 | 77 | #else /* TOFILE */ 78 | 79 | static void 80 | __load_verdaux(verdaux_mtype *dst, const verdaux_ftype *src, unsigned enc) { 81 | if (enc == ELFDATA2LSB) { 82 | dst->vda_name = __load_u32L(src->vda_name); 83 | dst->vda_next = __load_u32L(src->vda_next); 84 | } 85 | else { 86 | dst->vda_name = __load_u32M(src->vda_name); 87 | dst->vda_next = __load_u32M(src->vda_next); 88 | } 89 | } 90 | 91 | static void 92 | __load_verdef(verdef_mtype *dst, const verdef_ftype *src, unsigned enc) { 93 | if (enc == ELFDATA2LSB) { 94 | dst->vd_version = __load_u16L(src->vd_version); 95 | dst->vd_flags = __load_u16L(src->vd_flags); 96 | dst->vd_ndx = __load_u16L(src->vd_ndx); 97 | dst->vd_cnt = __load_u16L(src->vd_cnt); 98 | dst->vd_hash = __load_u32L(src->vd_hash); 99 | dst->vd_aux = __load_u32L(src->vd_aux); 100 | dst->vd_next = __load_u32L(src->vd_next); 101 | } 102 | else { 103 | dst->vd_version = __load_u16M(src->vd_version); 104 | dst->vd_flags = __load_u16M(src->vd_flags); 105 | dst->vd_ndx = __load_u16M(src->vd_ndx); 106 | dst->vd_cnt = __load_u16M(src->vd_cnt); 107 | dst->vd_hash = __load_u32M(src->vd_hash); 108 | dst->vd_aux = __load_u32M(src->vd_aux); 109 | dst->vd_next = __load_u32M(src->vd_next); 110 | } 111 | } 112 | 113 | typedef verdaux_ftype verdaux_stype; 114 | typedef verdaux_mtype verdaux_dtype; 115 | typedef verdef_ftype verdef_stype; 116 | typedef verdef_mtype verdef_dtype; 117 | typedef align_ftype verdef_atype; 118 | 119 | #define copy_verdaux_srctotmp(d, s, e) __load_verdaux((d), (s), (e)) 120 | #define copy_verdaux_tmptodst(d, s, e) (*(d) = *(s)) 121 | #define copy_verdef_srctotmp(d, s, e) __load_verdef((d), (s), (e)) 122 | #define copy_verdef_tmptodst(d, s, e) (*(d) = *(s)) 123 | 124 | #define translator_suffix _tom 125 | 126 | #endif /* TOFILE */ 127 | 128 | #define cat3(a,b,c) a##b##c 129 | #define xlt3(p,e,s) cat3(p,e,s) 130 | #define xltprefix(x) xlt3(x,_,class_suffix) 131 | #define translator(x,e) xlt3(xltprefix(_elf_##x),e,translator_suffix) 132 | 133 | static size_t 134 | xlt_verdef(unsigned char *dst, const unsigned char *src, size_t n, unsigned enc) { 135 | size_t off; 136 | 137 | if (sizeof(verdef_stype) != sizeof(verdef_dtype) 138 | || sizeof(verdaux_stype) != sizeof(verdaux_dtype)) { 139 | /* never happens for ELF v1 and Verneed v1 */ 140 | seterr(ERROR_UNIMPLEMENTED); 141 | return (size_t)-1; 142 | } 143 | /* size translation shortcut */ 144 | if (dst == NULL) { 145 | return n; 146 | } 147 | if (src == NULL) { 148 | seterr(ERROR_NULLBUF); 149 | return (size_t)-1; 150 | } 151 | off = 0; 152 | while (off + sizeof(verdef_stype) <= n) { 153 | const verdef_stype *svd; 154 | verdef_dtype *dvd; 155 | verdef_mtype vd; 156 | size_t acount; 157 | size_t aoff; 158 | 159 | /* 160 | * check for proper alignment 161 | */ 162 | if (off % sizeof(verdef_atype)) { 163 | seterr(ERROR_VERDEF_FORMAT); 164 | return (size_t)-1; 165 | } 166 | /* 167 | * copy and check src 168 | */ 169 | svd = (verdef_stype*)(src + off); 170 | dvd = (verdef_dtype*)(dst + off); 171 | copy_verdef_srctotmp(&vd, svd, enc); 172 | if (vd.vd_version < 1 173 | || vd.vd_version > VER_DEF_CURRENT) { 174 | seterr(ERROR_VERDEF_VERSION); 175 | return (size_t)-1; 176 | } 177 | if (vd.vd_cnt < 1 178 | || vd.vd_aux == 0) { 179 | seterr(ERROR_VERDEF_FORMAT); 180 | return (size_t)-1; 181 | } 182 | copy_verdef_tmptodst(dvd, &vd, enc); 183 | /* 184 | * copy aux array 185 | */ 186 | aoff = off + vd.vd_aux; 187 | for (acount = 0; acount < vd.vd_cnt; acount++) { 188 | const verdaux_stype *svda; 189 | verdaux_dtype *dvda; 190 | verdaux_mtype vda; 191 | 192 | /* 193 | * are we still inside the buffer limits? 194 | */ 195 | if (aoff + sizeof(verdaux_stype) > n) { 196 | break; 197 | } 198 | /* 199 | * check for proper alignment 200 | */ 201 | if (aoff % sizeof(verdef_atype)) { 202 | seterr(ERROR_VERDEF_FORMAT); 203 | return (size_t)-1; 204 | } 205 | /* 206 | * copy and check src 207 | */ 208 | svda = (verdaux_stype*)(src + aoff); 209 | dvda = (verdaux_dtype*)(dst + aoff); 210 | copy_verdaux_srctotmp(&vda, svda, enc); 211 | copy_verdaux_tmptodst(dvda, &vda, enc); 212 | /* 213 | * advance to next verdaux 214 | */ 215 | if (vda.vda_next == 0) { 216 | /* end of list */ 217 | break; 218 | } 219 | aoff += vda.vda_next; 220 | } 221 | /* 222 | * advance to next verdef 223 | */ 224 | if (vd.vd_next == 0) { 225 | /* end of list */ 226 | break; 227 | } 228 | off += vd.vd_next; 229 | } 230 | return n; 231 | } 232 | 233 | size_t 234 | translator(verdef,L11)(unsigned char *dst, const unsigned char *src, size_t n) { 235 | return xlt_verdef(dst, src, n, ELFDATA2LSB); 236 | } 237 | 238 | size_t 239 | translator(verdef,M11)(unsigned char *dst, const unsigned char *src, size_t n) { 240 | return xlt_verdef(dst, src, n, ELFDATA2MSB); 241 | } 242 | -------------------------------------------------------------------------------- /lib/verdef_32_tof.c: -------------------------------------------------------------------------------- 1 | /* 2 | verdef_32_tof.c - copy 32-bit versioning information. 3 | Copyright (C) 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #if __LIBELF_SYMBOL_VERSIONS 25 | 26 | #ifndef lint 27 | static const char rcsid[] = "@(#) $Id: verdef_32_tof.c,v 1.5 2008/05/23 08:15:35 michael Exp $"; 28 | #endif /* lint */ 29 | 30 | typedef Elf32_Verdaux verdaux_mtype; 31 | typedef Elf32_Verdef verdef_mtype; 32 | typedef Elf32_Vernaux vernaux_mtype; 33 | typedef Elf32_Verneed verneed_mtype; 34 | typedef Elf32_Word align_mtype; 35 | 36 | typedef __ext_Elf32_Verdaux verdaux_ftype; 37 | typedef __ext_Elf32_Verdef verdef_ftype; 38 | typedef __ext_Elf32_Vernaux vernaux_ftype; 39 | typedef __ext_Elf32_Verneed verneed_ftype; 40 | typedef __ext_Elf32_Word align_ftype; 41 | 42 | #define class_suffix 32 43 | 44 | #undef TOFILE 45 | #define TOFILE 1 46 | 47 | /* 48 | * Include shared code 49 | */ 50 | #include "verdef.h" 51 | #include "verneed.h" 52 | 53 | #endif /* __LIBELF_SYMBOL_VERSIONS */ 54 | -------------------------------------------------------------------------------- /lib/verdef_32_tom.c: -------------------------------------------------------------------------------- 1 | /* 2 | verdef_32_tom.c - copy 32-bit versioning information. 3 | Copyright (C) 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #if __LIBELF_SYMBOL_VERSIONS 25 | 26 | #ifndef lint 27 | static const char rcsid[] = "@(#) $Id: verdef_32_tom.c,v 1.5 2008/05/23 08:15:35 michael Exp $"; 28 | #endif /* lint */ 29 | 30 | typedef Elf32_Verdaux verdaux_mtype; 31 | typedef Elf32_Verdef verdef_mtype; 32 | typedef Elf32_Vernaux vernaux_mtype; 33 | typedef Elf32_Verneed verneed_mtype; 34 | typedef Elf32_Word align_mtype; 35 | 36 | typedef __ext_Elf32_Verdaux verdaux_ftype; 37 | typedef __ext_Elf32_Verdef verdef_ftype; 38 | typedef __ext_Elf32_Vernaux vernaux_ftype; 39 | typedef __ext_Elf32_Verneed verneed_ftype; 40 | typedef __ext_Elf32_Word align_ftype; 41 | 42 | #define class_suffix 32 43 | 44 | #undef TOFILE 45 | #define TOFILE 0 46 | 47 | /* 48 | * Include shared code 49 | */ 50 | #include "verdef.h" 51 | #include "verneed.h" 52 | 53 | #endif /* __LIBELF_SYMBOL_VERSIONS */ 54 | -------------------------------------------------------------------------------- /lib/verdef_64_tof.c: -------------------------------------------------------------------------------- 1 | /* 2 | verdef_64_tof.c - copy 64-bit versioning information. 3 | Copyright (C) 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #if __LIBELF64 && __LIBELF_SYMBOL_VERSIONS 25 | 26 | #ifndef lint 27 | static const char rcsid[] = "@(#) $Id: verdef_64_tof.c,v 1.5 2008/05/23 08:15:35 michael Exp $"; 28 | #endif /* lint */ 29 | 30 | typedef Elf64_Verdaux verdaux_mtype; 31 | typedef Elf64_Verdef verdef_mtype; 32 | typedef Elf64_Vernaux vernaux_mtype; 33 | typedef Elf64_Verneed verneed_mtype; 34 | typedef Elf64_Word align_mtype; 35 | 36 | typedef __ext_Elf64_Verdaux verdaux_ftype; 37 | typedef __ext_Elf64_Verdef verdef_ftype; 38 | typedef __ext_Elf64_Vernaux vernaux_ftype; 39 | typedef __ext_Elf64_Verneed verneed_ftype; 40 | typedef __ext_Elf64_Word align_ftype; 41 | 42 | #define class_suffix 64 43 | 44 | #undef TOFILE 45 | #define TOFILE 1 46 | 47 | /* 48 | * Include shared code 49 | */ 50 | #include "verdef.h" 51 | #include "verneed.h" 52 | 53 | #endif /* __LIBELF64 && __LIBELF_SYMBOL_VERSIONS */ 54 | -------------------------------------------------------------------------------- /lib/verdef_64_tom.c: -------------------------------------------------------------------------------- 1 | /* 2 | verdef_64_tom.c - copy 64-bit versioning information. 3 | Copyright (C) 2001 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #if __LIBELF64 && __LIBELF_SYMBOL_VERSIONS 25 | 26 | #ifndef lint 27 | static const char rcsid[] = "@(#) $Id: verdef_64_tom.c,v 1.5 2008/05/23 08:15:35 michael Exp $"; 28 | #endif /* lint */ 29 | 30 | typedef Elf64_Verdaux verdaux_mtype; 31 | typedef Elf64_Verdef verdef_mtype; 32 | typedef Elf64_Vernaux vernaux_mtype; 33 | typedef Elf64_Verneed verneed_mtype; 34 | typedef Elf64_Word align_mtype; 35 | 36 | typedef __ext_Elf64_Verdaux verdaux_ftype; 37 | typedef __ext_Elf64_Verdef verdef_ftype; 38 | typedef __ext_Elf64_Vernaux vernaux_ftype; 39 | typedef __ext_Elf64_Verneed verneed_ftype; 40 | typedef __ext_Elf64_Word align_ftype; 41 | 42 | #define class_suffix 64 43 | 44 | #undef TOFILE 45 | #define TOFILE 0 46 | 47 | /* 48 | * Include shared code 49 | */ 50 | #include "verdef.h" 51 | #include "verneed.h" 52 | 53 | #endif /* __LIBELF64 && __LIBELF_SYMBOL_VERSIONS */ 54 | -------------------------------------------------------------------------------- /lib/version.c: -------------------------------------------------------------------------------- 1 | /* 2 | * version.c - implementation of the elf_version(3) function. 3 | * Copyright (C) 1995 - 1998, 2007 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: version.c,v 1.8 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | unsigned 27 | elf_version(unsigned ver) { 28 | const char *s; 29 | unsigned tmp; 30 | 31 | if ((s = getenv("LIBELF_SANITY_CHECKS"))) { 32 | _elf_sanity_checks = (int)strtol(s, (char**)NULL, 0); 33 | } 34 | if (ver == EV_NONE) { 35 | return EV_CURRENT; 36 | } 37 | if (!valid_version(ver)) { 38 | seterr(ERROR_UNKNOWN_VERSION); 39 | return EV_NONE; 40 | } 41 | tmp = _elf_version == EV_NONE ? EV_CURRENT : _elf_version; 42 | _elf_version = ver; 43 | return tmp; 44 | } 45 | -------------------------------------------------------------------------------- /lib/x.elfext.c: -------------------------------------------------------------------------------- 1 | /* 2 | * x.elfext.c -- handle ELF format extensions 3 | * Copyright (C) 2002 - 2006 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: x.elfext.c,v 1.5 2009/07/07 17:57:43 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | int 27 | elf_getphdrnum(Elf *elf, size_t *resultp) { 28 | if (!elf) { 29 | return -1; 30 | } 31 | elf_assert(elf->e_magic == ELF_MAGIC); 32 | if (elf->e_kind != ELF_K_ELF) { 33 | seterr(ERROR_NOTELF); 34 | return -1; 35 | } 36 | if (!elf->e_ehdr && !_elf_cook(elf)) { 37 | return -1; 38 | } 39 | if (resultp) { 40 | *resultp = elf->e_phnum; 41 | } 42 | return 0; 43 | } 44 | 45 | int 46 | elf_getshdrnum(Elf *elf, size_t *resultp) { 47 | size_t num = 0; 48 | Elf_Scn *scn; 49 | 50 | if (!elf) { 51 | return -1; 52 | } 53 | elf_assert(elf->e_magic == ELF_MAGIC); 54 | if (elf->e_kind != ELF_K_ELF) { 55 | seterr(ERROR_NOTELF); 56 | return -1; 57 | } 58 | if (!elf->e_ehdr && !_elf_cook(elf)) { 59 | return -1; 60 | } 61 | if ((scn = elf->e_scn_n)) { 62 | num = scn->s_index + 1; 63 | } 64 | if (resultp) { 65 | *resultp = num; 66 | } 67 | return 0; 68 | } 69 | 70 | int 71 | elf_getshdrstrndx(Elf *elf, size_t *resultp) { 72 | size_t num = 0; 73 | size_t dummy; 74 | Elf_Scn *scn; 75 | 76 | if (!elf) { 77 | return -1; 78 | } 79 | elf_assert(elf->e_magic == ELF_MAGIC); 80 | if (resultp == NULL) { 81 | resultp = &dummy; /* handle NULL pointer gracefully */ 82 | } 83 | if (elf->e_kind != ELF_K_ELF) { 84 | seterr(ERROR_NOTELF); 85 | return -1; 86 | } 87 | if (!elf->e_ehdr && !_elf_cook(elf)) { 88 | return -1; 89 | } 90 | if (elf->e_class == ELFCLASS32) { 91 | num = ((Elf32_Ehdr*)elf->e_ehdr)->e_shstrndx; 92 | } 93 | #if __LIBELF64 94 | else if (elf->e_class == ELFCLASS64) { 95 | num = ((Elf64_Ehdr*)elf->e_ehdr)->e_shstrndx; 96 | } 97 | #endif /* __LIBELF64 */ 98 | else { 99 | if (valid_class(elf->e_class)) { 100 | seterr(ERROR_UNIMPLEMENTED); 101 | } 102 | else { 103 | seterr(ERROR_UNKNOWN_CLASS); 104 | } 105 | return -1; 106 | } 107 | if (num != SHN_XINDEX) { 108 | *resultp = num; 109 | return 0; 110 | } 111 | /* 112 | * look at first section header 113 | */ 114 | if (!(scn = elf->e_scn_1)) { 115 | seterr(ERROR_NOSUCHSCN); 116 | return -1; 117 | } 118 | elf_assert(scn->s_magic == SCN_MAGIC); 119 | #if __LIBELF64 120 | if (elf->e_class == ELFCLASS64) { 121 | *resultp = scn->s_shdr64.sh_link; 122 | return 0; 123 | } 124 | #endif /* __LIBELF64 */ 125 | *resultp = scn->s_shdr32.sh_link; 126 | return 0; 127 | } 128 | 129 | int 130 | elf_getphnum(Elf *elf, size_t *resultp) { 131 | return elf_getphdrnum(elf, resultp) ? LIBELF_FAILURE : LIBELF_SUCCESS; 132 | } 133 | 134 | int 135 | elf_getshnum(Elf *elf, size_t *resultp) { 136 | return elf_getshdrnum(elf, resultp) ? LIBELF_FAILURE : LIBELF_SUCCESS; 137 | } 138 | 139 | int 140 | elf_getshstrndx(Elf *elf, size_t *resultp) { 141 | return elf_getshdrstrndx(elf, resultp) ? LIBELF_FAILURE : LIBELF_SUCCESS; 142 | } 143 | 144 | int 145 | elfx_update_shstrndx(Elf *elf, size_t value) { 146 | size_t extvalue = 0; 147 | Elf_Scn *scn; 148 | 149 | if (!elf) { 150 | return LIBELF_FAILURE; 151 | } 152 | elf_assert(elf->e_magic == ELF_MAGIC); 153 | if (value >= SHN_LORESERVE) { 154 | extvalue = value; 155 | value = SHN_XINDEX; 156 | } 157 | if (elf->e_kind != ELF_K_ELF) { 158 | seterr(ERROR_NOTELF); 159 | return LIBELF_FAILURE; 160 | } 161 | if (!elf->e_ehdr && !_elf_cook(elf)) { 162 | return LIBELF_FAILURE; 163 | } 164 | if (!(scn = _elf_first_scn(elf))) { 165 | return LIBELF_FAILURE; 166 | } 167 | elf_assert(scn->s_magic == SCN_MAGIC); 168 | if (elf->e_class == ELFCLASS32) { 169 | ((Elf32_Ehdr*)elf->e_ehdr)->e_shstrndx = value; 170 | scn->s_shdr32.sh_link = extvalue; 171 | } 172 | #if __LIBELF64 173 | else if (elf->e_class == ELFCLASS64) { 174 | ((Elf64_Ehdr*)elf->e_ehdr)->e_shstrndx = value; 175 | scn->s_shdr64.sh_link = extvalue; 176 | } 177 | #endif /* __LIBELF64 */ 178 | else { 179 | if (valid_class(elf->e_class)) { 180 | seterr(ERROR_UNIMPLEMENTED); 181 | } 182 | else { 183 | seterr(ERROR_UNKNOWN_CLASS); 184 | } 185 | return LIBELF_FAILURE; 186 | } 187 | elf->e_ehdr_flags |= ELF_F_DIRTY; 188 | scn->s_shdr_flags |= ELF_F_DIRTY; 189 | return LIBELF_SUCCESS; 190 | } 191 | -------------------------------------------------------------------------------- /lib/x.movscn.c: -------------------------------------------------------------------------------- 1 | /* 2 | x.movscn.c - implementation of the elfx_movscn(3) function. 3 | Copyright (C) 1995 - 2001, 2003 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: x.movscn.c,v 1.14 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | size_t 27 | elfx_movscn(Elf *elf, Elf_Scn *scn, Elf_Scn *after) { 28 | Elf_Scn *prev; 29 | Elf_Scn *tmp; 30 | int off; 31 | 32 | if (!elf || !scn || !after) { 33 | return SHN_UNDEF; 34 | } 35 | elf_assert(elf->e_magic == ELF_MAGIC); 36 | if (elf->e_kind != ELF_K_ELF) { 37 | seterr(ERROR_NOTELF); 38 | return SHN_UNDEF; 39 | } 40 | elf_assert(scn->s_magic == SCN_MAGIC); 41 | elf_assert(after->s_magic == SCN_MAGIC); 42 | if (scn->s_elf != elf || after->s_elf != elf) { 43 | seterr(ERROR_ELFSCNMISMATCH); 44 | return SHN_UNDEF; 45 | } 46 | elf_assert(elf->e_scn_1); 47 | if (scn == elf->e_scn_1) { 48 | seterr(ERROR_NULLSCN); 49 | return SHN_UNDEF; 50 | } 51 | if (scn == after || scn == after->s_link) { 52 | /* nothing to do */ 53 | return scn->s_index; 54 | } 55 | 56 | /* 57 | * Find previous section. 58 | */ 59 | prev = NULL; 60 | for (tmp = elf->e_scn_1; tmp->s_link; tmp = tmp->s_link) { 61 | if (tmp->s_link == scn) { 62 | prev = tmp; 63 | break; 64 | } 65 | } 66 | elf_assert(prev != NULL); 67 | 68 | /* 69 | * Update section indices 70 | */ 71 | off = 0; 72 | for (tmp = elf->e_scn_1; tmp; tmp = tmp->s_link) { 73 | if (off) { 74 | tmp->s_index += off; 75 | } 76 | if (tmp == after) { 77 | off++; 78 | } 79 | else if (tmp == scn) { 80 | off--; 81 | } 82 | } 83 | elf_assert(off == 0); 84 | 85 | /* 86 | * Move section. 87 | */ 88 | prev->s_link = scn->s_link; 89 | scn->s_link = after->s_link; 90 | after->s_link = scn; 91 | scn->s_index = after->s_index + 1; 92 | if (elf->e_scn_n == scn) { 93 | elf->e_scn_n = prev; 94 | } 95 | else if (elf->e_scn_n == after) { 96 | elf->e_scn_n = scn; 97 | } 98 | 99 | #if ENABLE_DEBUG 100 | /* 101 | * Check section indices 102 | */ 103 | tmp = elf->e_scn_1; 104 | elf_assert(tmp->s_index == 0); 105 | while (tmp->s_link) { 106 | elf_assert(tmp->s_link->s_index == tmp->s_index + 1); 107 | tmp = tmp->s_link; 108 | } 109 | #endif /* ENABLE_DEBUG */ 110 | 111 | return scn->s_index; 112 | } 113 | -------------------------------------------------------------------------------- /lib/x.remscn.c: -------------------------------------------------------------------------------- 1 | /* 2 | x.remscn.c - implementation of the elfx_remscn(3) function. 3 | Copyright (C) 1995 - 2001, 2003 Michael Riepe 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Library General Public 7 | License as published by the Free Software Foundation; either 8 | version 2 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Library General Public License for more details. 14 | 15 | You should have received a copy of the GNU Library General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #include 21 | 22 | #ifndef lint 23 | static const char rcsid[] = "@(#) $Id: x.remscn.c,v 1.15 2008/05/23 08:15:35 michael Exp $"; 24 | #endif /* lint */ 25 | 26 | size_t 27 | elfx_remscn(Elf *elf, Elf_Scn *scn) { 28 | Elf_Scn *pscn; 29 | Scn_Data *sd; 30 | Scn_Data *tmp; 31 | size_t index; 32 | 33 | if (!elf || !scn) { 34 | return SHN_UNDEF; 35 | } 36 | elf_assert(elf->e_magic == ELF_MAGIC); 37 | if (elf->e_kind != ELF_K_ELF) { 38 | seterr(ERROR_NOTELF); 39 | return SHN_UNDEF; 40 | } 41 | elf_assert(scn->s_magic == SCN_MAGIC); 42 | elf_assert(elf->e_ehdr); 43 | if (scn->s_elf != elf) { 44 | seterr(ERROR_ELFSCNMISMATCH); 45 | return SHN_UNDEF; 46 | } 47 | elf_assert(elf->e_scn_1); 48 | if (scn == elf->e_scn_1) { 49 | seterr(ERROR_NULLSCN); 50 | return SHN_UNDEF; 51 | } 52 | 53 | /* 54 | * Find previous section. 55 | */ 56 | for (pscn = elf->e_scn_1; pscn->s_link; pscn = pscn->s_link) { 57 | if (pscn->s_link == scn) { 58 | break; 59 | } 60 | } 61 | if (pscn->s_link != scn) { 62 | seterr(ERROR_ELFSCNMISMATCH); 63 | return SHN_UNDEF; 64 | } 65 | 66 | /* 67 | * Unlink section. 68 | */ 69 | if (elf->e_scn_n == scn) { 70 | elf->e_scn_n = pscn; 71 | } 72 | pscn->s_link = scn->s_link; 73 | index = scn->s_index; 74 | 75 | /* 76 | * Free section descriptor and data. 77 | */ 78 | for (sd = scn->s_data_1; sd; sd = tmp) { 79 | elf_assert(sd->sd_magic == DATA_MAGIC); 80 | elf_assert(sd->sd_scn == scn); 81 | tmp = sd->sd_link; 82 | if (sd->sd_free_data && sd->sd_memdata) { 83 | free(sd->sd_memdata); 84 | } 85 | if (sd->sd_freeme) { 86 | free(sd); 87 | } 88 | } 89 | if ((sd = scn->s_rawdata)) { 90 | elf_assert(sd->sd_magic == DATA_MAGIC); 91 | elf_assert(sd->sd_scn == scn); 92 | if (sd->sd_free_data && sd->sd_memdata) { 93 | free(sd->sd_memdata); 94 | } 95 | if (sd->sd_freeme) { 96 | free(sd); 97 | } 98 | } 99 | if (scn->s_freeme) { 100 | elf_assert(scn->s_index > 0); 101 | free(scn); 102 | } 103 | 104 | /* 105 | * Adjust section indices. 106 | */ 107 | for (scn = pscn->s_link; scn; scn = scn->s_link) { 108 | elf_assert(scn->s_index > index); 109 | scn->s_index--; 110 | } 111 | 112 | /* 113 | * Adjust section count in ELF header 114 | */ 115 | if (_elf_update_shnum(elf, elf->e_scn_n->s_index + 1)) { 116 | return SHN_UNDEF; 117 | } 118 | return index; 119 | } 120 | -------------------------------------------------------------------------------- /libelf.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: @PACKAGE@ 7 | Description: ELF object file access library 8 | Version: @VERSION@ 9 | Requires: 10 | Conflicts: 11 | Libs: -L${libdir} -lelf 12 | Cflags: -I${includedir}/libelf 13 | -------------------------------------------------------------------------------- /mkinstalldirs: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Make directory hierarchy. 3 | # Written by Noah Friedman 4 | # Public domain. 5 | 6 | defaultIFS=' 7 | ' 8 | IFS="${IFS-${defaultIFS}}" 9 | 10 | errstatus=0 11 | 12 | for file in ${1+"$@"} ; do 13 | oIFS="${IFS}" 14 | # Some sh's can't handle IFS=/ for some reason. 15 | IFS='%' 16 | set - `echo ${file} | sed -e 's@/@%@g' -e 's@^%@/@'` 17 | IFS="${oIFS}" 18 | 19 | pathcomp='' 20 | 21 | for d in ${1+"$@"} ; do 22 | pathcomp="${pathcomp}${d}" 23 | 24 | if test ! -d "${pathcomp}"; then 25 | echo "mkdir $pathcomp" 1>&2 26 | mkdir "${pathcomp}" || errstatus=$? 27 | fi 28 | 29 | pathcomp="${pathcomp}/" 30 | done 31 | done 32 | 33 | exit $errstatus 34 | 35 | # eof 36 | -------------------------------------------------------------------------------- /po/Makefile.in: -------------------------------------------------------------------------------- 1 | # po/Makefile for libelf. 2 | # Copyright (C) 1995 - 2006 Michael Riepe 3 | # 4 | # This library is free software; you can redistribute it and/or 5 | # modify it under the terms of the GNU Library General Public 6 | # License as published by the Free Software Foundation; either 7 | # version 2 of the License, or (at your option) any later version. 8 | # 9 | # This library is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | # Library General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU Library General Public 15 | # License along with this library; if not, write to the Free Software 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 17 | 18 | # @(#) $Id: Makefile.in,v 1.18 2008/05/23 08:16:46 michael Exp $ 19 | 20 | instroot = 21 | 22 | prefix = @prefix@ 23 | exec_prefix = @exec_prefix@ 24 | localedir = @localedir@ 25 | 26 | CC = @CC@ 27 | RM = rm -f 28 | MV = mv -f 29 | INSTALL = @INSTALL@ 30 | INSTALL_DATA = @INSTALL_DATA@ 31 | 32 | GENCAT = @GENCAT@ 33 | GMSGFMT = @GMSGFMT@ 34 | MSGFMT = @MSGFMT@ 35 | XGETTEXT = @XGETTEXT@ 36 | MSGMERGE = @MSGMERGE@ 37 | 38 | CFLAGS = @CFLAGS@ 39 | CPPFLAGS = @CPPFLAGS@ 40 | DEFS = -DHAVE_CONFIG_H 41 | LDFLAGS = @LDFLAGS@ 42 | LIBS = @LIBS@ 43 | LIBINTL = @LIBINTL@ 44 | 45 | # no user serviceable parts below 46 | 47 | PACKAGE = @PACKAGE@ 48 | VERSION = @VERSION@ 49 | 50 | SHELL = /bin/sh 51 | @SET_MAKE@ 52 | 53 | srcdir = @srcdir@ 54 | top_srcdir = @top_srcdir@ 55 | VPATH = @srcdir@ 56 | 57 | topdir = .. 58 | subdir = po 59 | 60 | .SUFFIXES: 61 | .SUFFIXES: .po .mo .gmo .msg .cat 62 | 63 | .po.mo: 64 | @$(RM) $@ 65 | $(MSGFMT) -o $@ $< 66 | 67 | .po.gmo: 68 | file=$(srcdir)/`echo $*|sed 's,.*/,,'`.gmo; \ 69 | $(RM) $$file && $(GMSGFMT) -o $$file $< 70 | 71 | .msg.cat: 72 | @$(RM) $@ 73 | $(GENCAT) $@ $< 74 | 75 | POFILES = @POFILES@ 76 | GMOFILES = @GMOFILES@ 77 | MSGFILES = @MSGFILES@ 78 | 79 | DISTFILES = \ 80 | gmo2msg.c Makefile.in $(PACKAGE).pot stamp-po \ 81 | $(POFILES) $(GMOFILES) $(MSGFILES) 82 | 83 | POTFILES = $(top_srcdir)/lib/errors.h 84 | 85 | CATALOGS = @CATALOGS@ 86 | CATOBJEXT = @CATOBJEXT@ 87 | INSTOBJEXT = @INSTOBJEXT@ 88 | 89 | all: $(CATALOGS) 90 | 91 | check: 92 | 93 | install: all install-data 94 | 95 | install-data: $(top_srcdir)/mkinstalldirs 96 | catalogs="$(CATALOGS)"; for cat in $$catalogs; do \ 97 | lang=`echo $$cat | sed 's,$(CATOBJEXT)$$,,'`; \ 98 | dir=$(localedir)/$$lang/LC_MESSAGES; \ 99 | $(SHELL) $(top_srcdir)/mkinstalldirs $(instroot)$$dir; \ 100 | if test -r $$cat; then \ 101 | $(INSTALL_DATA) $$cat $(instroot)$$dir/$(PACKAGE)$(INSTOBJEXT); \ 102 | else \ 103 | $(INSTALL_DATA) $(srcdir)/$$cat $(instroot)$$dir/$(PACKAGE)$(INSTOBJEXT); \ 104 | fi; \ 105 | done 106 | 107 | uninstall: 108 | catalogs="$(CATALOGS)"; for cat in $$catalogs; do \ 109 | lang=`echo $$cat | sed 's,$(CATOBJEXT)$$,,'`; \ 110 | $(RM) $(instroot)$(localedir)/$$lang/LC_MESSAGES/$(PACKAGE)$(INSTOBJEXT); \ 111 | done 112 | 113 | mostlyclean: 114 | $(RM) core core.* $(PACKAGE).po *.po.tmp 115 | 116 | clean: mostlyclean 117 | 118 | distclean: clean 119 | $(RM) gmo2msg *.mo *.cat 120 | $(RM) Makefile 121 | 122 | maintainer-clean: distclean 123 | $(RM) stamp-po 124 | 125 | $(PACKAGE).pot: $(POTFILES) 126 | $(XGETTEXT) -c -d$(PACKAGE) -k_ $(POTFILES) 127 | if cmp -s $(PACKAGE).po $(srcdir)/$(PACKAGE).pot; then \ 128 | $(RM) $(PACKAGE).po; \ 129 | else \ 130 | $(RM) $(srcdir)/$(PACKAGE).pot && \ 131 | $(MV) $(PACKAGE).po $(srcdir)/$(PACKAGE).pot; \ 132 | fi 133 | 134 | update-po: stamp-po 135 | stamp-po: $(PACKAGE).pot 136 | pofiles="$(POFILES)"; cd $(srcdir) && for po in $$pofiles; do \ 137 | $(RM) $$po.tmp; \ 138 | if $(MSGMERGE) $$po $(PACKAGE).pot > $$po.tmp; then \ 139 | $(RM) $$po; \ 140 | $(MV) $$po.tmp $$po; \ 141 | else \ 142 | echo "update for $$po failed!"; \ 143 | $(RM) $$po.tmp; \ 144 | fi; \ 145 | done 146 | $(RM) $@ && echo timestamp > $@ 147 | 148 | # Create X/Open message catalog sources from .gmo files. 149 | 150 | .gmo.msg: 151 | $(MAKE) $(srcdir)/gmo2msg 152 | cd $(srcdir) && ./gmo2msg `echo $*|sed 's,.*/,,'` 153 | 154 | .SUFFIXES: .c 155 | 156 | .c: 157 | @$(RM) $@ 158 | $(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $(XCFLAGS) \ 159 | $(LDFLAGS) $*.c $(LIBS) $(LIBINTL) -o $@ 160 | 161 | INCLUDES = -I$(topdir) -I. -I$(topdir)/lib -I$(srcdir) -I$(top_srcdir)/lib 162 | 163 | # maintainer only 164 | 165 | MAINT = @MAINT@ 166 | 167 | distdir = $(PACKAGE)-$(VERSION) 168 | distsubdir = $(topdir)/$(distdir)/$(subdir) 169 | $(MAINT)dist: update-po $(DISTFILES) 170 | if test -d $(distsubdir); then true; else mkdir $(distsubdir); fi 171 | files="$(DISTFILES)"; for file in $$files; do \ 172 | ln $(srcdir)/$$file $(distsubdir) >/dev/null 2>&1 || \ 173 | cp -p $(srcdir)/$$file $(distsubdir) || exit 1; \ 174 | done 175 | 176 | # For the justification of the following Makefile rules, see node 177 | # `Automatic Remaking' in GNU Autoconf documentation. 178 | 179 | $(MAINT)Makefile: Makefile.in $(topdir)/config.status 180 | cd $(topdir) && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= ./config.status 181 | 182 | # Tell versions [3.59,3.63) of GNU make not to export all variables. 183 | # Otherwise a system limit (for SysV at least) may be exceeded. 184 | .NOEXPORT: 185 | -------------------------------------------------------------------------------- /po/de.gmo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WolfgangSt/libelf/dbbf0eddb1f7d243f5f909cba13597ad76fc4fcb/po/de.gmo -------------------------------------------------------------------------------- /po/de.msg: -------------------------------------------------------------------------------- 1 | $set 1 Automatically created from de.gmo by gmo2msg 2 | -------------------------------------------------------------------------------- /po/de.po: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WolfgangSt/libelf/dbbf0eddb1f7d243f5f909cba13597ad76fc4fcb/po/de.po -------------------------------------------------------------------------------- /po/gmo2msg.c: -------------------------------------------------------------------------------- 1 | /* 2 | * gmo2msg.c - create X/Open message source file for libelf. 3 | * Copyright (C) 1996 - 2005 Michael Riepe 4 | * 5 | * This library is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Library General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 2 of the License, or (at your option) any later version. 9 | * 10 | * This library is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Library General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Library General Public 16 | * License along with this library; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 18 | */ 19 | 20 | #ifndef lint 21 | static const char rcsid[] = "@(#) $Id: gmo2msg.c,v 1.11 2008/05/23 08:16:46 michael Exp $"; 22 | #endif /* lint */ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #define DOMAIN "libelf" 31 | 32 | static const char *msgs[] = { 33 | #define __err__(a,b) b, 34 | #include 35 | #undef __err__ 36 | }; 37 | 38 | int 39 | main(int argc, char **argv) { 40 | char buf[1024], *lang, *progname, *s; 41 | unsigned i; 42 | FILE *fp; 43 | 44 | setlocale(LC_ALL, ""); 45 | 46 | if (*argv && (progname = strrchr(argv[0], '/'))) { 47 | progname++; 48 | } 49 | else if (!(progname = *argv)) { 50 | progname = "gmo2msg"; 51 | } 52 | 53 | if (argc <= 1 || !(lang = argv[1])) { 54 | fprintf(stderr, "Usage: gmo2msg \n"); 55 | exit(1); 56 | } 57 | 58 | /* 59 | * Fool gettext... 60 | */ 61 | unlink(DOMAIN ".mo"); 62 | unlink("LC_MESSAGES"); 63 | unlink(lang); 64 | sprintf(buf, "%s.gmo", lang); 65 | if (link(buf, DOMAIN ".mo") == -1) { 66 | fprintf(stderr, "Cannot link %s to " DOMAIN ".mo\n", buf); 67 | perror(""); 68 | exit(1); 69 | } 70 | symlink(".", "LC_MESSAGES"); 71 | symlink(".", lang); 72 | textdomain(DOMAIN); 73 | getcwd(buf, sizeof(buf)); 74 | bindtextdomain(DOMAIN, buf); 75 | 76 | sprintf(buf, "%s.msg", lang); 77 | unlink(buf); 78 | if (!(fp = fopen(buf, "w"))) { 79 | perror(buf); 80 | exit(1); 81 | } 82 | 83 | fprintf(fp, "$set 1 Automatically created from %s.gmo by %s\n", lang, progname); 84 | 85 | /* 86 | * Translate messages. 87 | */ 88 | setlocale(LC_MESSAGES, lang); 89 | if ((s = gettext("")) && (s = strdup(s))) { 90 | s = strtok(s, "\n"); 91 | while (s) { 92 | fprintf(fp, "$ %s\n", s); 93 | s = strtok(NULL, "\n"); 94 | } 95 | } 96 | /* 97 | * Assume that messages contain printable ASCII characters ONLY. 98 | * That means no tabs, linefeeds etc. 99 | */ 100 | for (i = 0; i < sizeof(msgs)/sizeof(*msgs); i++) { 101 | s = gettext(msgs[i]); 102 | if (s != msgs[i] && strcmp(s, msgs[i]) != 0) { 103 | fprintf(fp, "$ \n$ Original message: %s\n", msgs[i]); 104 | fprintf(fp, "%u %s\n", i + 1, s); 105 | } 106 | } 107 | setlocale(LC_MESSAGES, ""); 108 | 109 | if (fclose(fp)) { 110 | perror("writing output file"); 111 | exit(1); 112 | } 113 | 114 | /* 115 | * Cleanup. 116 | */ 117 | unlink(DOMAIN ".mo"); 118 | unlink("LC_MESSAGES"); 119 | unlink(lang); 120 | exit(0); 121 | } 122 | -------------------------------------------------------------------------------- /po/libelf.pot: -------------------------------------------------------------------------------- 1 | # SOME DESCRIPTIVE TITLE. 2 | # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER 3 | # This file is distributed under the same license as the PACKAGE package. 4 | # FIRST AUTHOR , YEAR. 5 | # 6 | #, fuzzy 7 | msgid "" 8 | msgstr "" 9 | "Project-Id-Version: PACKAGE VERSION\n" 10 | "Report-Msgid-Bugs-To: \n" 11 | "POT-Creation-Date: 2008-05-23 10:18+0200\n" 12 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 13 | "Last-Translator: FULL NAME \n" 14 | "Language-Team: LANGUAGE \n" 15 | "MIME-Version: 1.0\n" 16 | "Content-Type: text/plain; charset=CHARSET\n" 17 | "Content-Transfer-Encoding: 8bit\n" 18 | 19 | #: ../lib/errors.h:25 20 | msgid "no error" 21 | msgstr "" 22 | 23 | #: ../lib/errors.h:26 24 | msgid "unknown error" 25 | msgstr "" 26 | 27 | #: ../lib/errors.h:27 28 | msgid "Internal error: unknown reason" 29 | msgstr "" 30 | 31 | #: ../lib/errors.h:28 32 | msgid "Internal error: not implemented" 33 | msgstr "" 34 | 35 | #: ../lib/errors.h:29 36 | msgid "Request error: cntl(ELF_C_FDREAD) on write-only file" 37 | msgstr "" 38 | 39 | #: ../lib/errors.h:30 40 | msgid "Request error: invalid ELF_C_* argument" 41 | msgstr "" 42 | 43 | #: ../lib/errors.h:31 44 | msgid "Request error: file descriptor disabled" 45 | msgstr "" 46 | 47 | #: ../lib/errors.h:32 48 | msgid "Request error: not an archive" 49 | msgstr "" 50 | 51 | #: ../lib/errors.h:33 52 | msgid "Request error: offset out of range" 53 | msgstr "" 54 | 55 | #: ../lib/errors.h:34 56 | msgid "Request error: unknown ELF version" 57 | msgstr "" 58 | 59 | #: ../lib/errors.h:35 60 | msgid "Request error: ELF_C_* argument does not match" 61 | msgstr "" 62 | 63 | #: ../lib/errors.h:36 64 | msgid "Request error: archive member begin() for writing" 65 | msgstr "" 66 | 67 | #: ../lib/errors.h:37 68 | msgid "Request error: archive/member file descriptor mismatch" 69 | msgstr "" 70 | 71 | #: ../lib/errors.h:38 72 | msgid "Request error: not an ELF file" 73 | msgstr "" 74 | 75 | #: ../lib/errors.h:39 76 | msgid "Request error: class file/memory mismatch" 77 | msgstr "" 78 | 79 | #: ../lib/errors.h:40 80 | msgid "Request error: invalid ELF_T_* argument" 81 | msgstr "" 82 | 83 | #: ../lib/errors.h:41 84 | msgid "Request error: unknown data encoding" 85 | msgstr "" 86 | 87 | #: ../lib/errors.h:42 88 | msgid "Request error: destination buffer too small" 89 | msgstr "" 90 | 91 | #: ../lib/errors.h:43 92 | msgid "Request error: d_buf is NULL" 93 | msgstr "" 94 | 95 | #: ../lib/errors.h:44 96 | msgid "Request error: unknown ELF class" 97 | msgstr "" 98 | 99 | #: ../lib/errors.h:45 100 | msgid "Request error: section does not belong to file" 101 | msgstr "" 102 | 103 | #: ../lib/errors.h:46 104 | msgid "Request error: no section at index" 105 | msgstr "" 106 | 107 | #: ../lib/errors.h:47 108 | msgid "Request error: can't manipulate null section" 109 | msgstr "" 110 | 111 | #: ../lib/errors.h:48 112 | msgid "Request error: data does not belong to section" 113 | msgstr "" 114 | 115 | #: ../lib/errors.h:49 116 | msgid "Request error: no string table" 117 | msgstr "" 118 | 119 | #: ../lib/errors.h:50 120 | msgid "Request error: string table offset out of range" 121 | msgstr "" 122 | 123 | #: ../lib/errors.h:51 124 | msgid "Request error: update(ELF_C_WRITE) on read-only file" 125 | msgstr "" 126 | 127 | #: ../lib/errors.h:52 128 | msgid "I/O error: seek" 129 | msgstr "" 130 | 131 | #: ../lib/errors.h:53 132 | msgid "I/O error: file too big for memory" 133 | msgstr "" 134 | 135 | #: ../lib/errors.h:54 136 | msgid "I/O error: raw read" 137 | msgstr "" 138 | 139 | #: ../lib/errors.h:55 140 | msgid "I/O error: get file size" 141 | msgstr "" 142 | 143 | #: ../lib/errors.h:56 144 | msgid "I/O error: output write" 145 | msgstr "" 146 | 147 | #: ../lib/errors.h:57 148 | msgid "I/O error: can't truncate output file" 149 | msgstr "" 150 | 151 | #: ../lib/errors.h:58 152 | msgid "Sequence error: must set ELF version first" 153 | msgstr "" 154 | 155 | #: ../lib/errors.h:59 156 | msgid "Sequence error: must create ELF header first" 157 | msgstr "" 158 | 159 | #: ../lib/errors.h:60 160 | msgid "Format error: reference outside file" 161 | msgstr "" 162 | 163 | #: ../lib/errors.h:61 164 | msgid "Format error: archive header truncated" 165 | msgstr "" 166 | 167 | #: ../lib/errors.h:62 168 | msgid "Format error: archive fmag" 169 | msgstr "" 170 | 171 | #: ../lib/errors.h:63 172 | msgid "Format error: archive header" 173 | msgstr "" 174 | 175 | #: ../lib/errors.h:64 176 | msgid "Format error: archive member truncated" 177 | msgstr "" 178 | 179 | #: ../lib/errors.h:65 180 | msgid "Format error: archive symbol table size" 181 | msgstr "" 182 | 183 | #: ../lib/errors.h:66 184 | msgid "Format error: archive string table" 185 | msgstr "" 186 | 187 | #: ../lib/errors.h:67 188 | msgid "Format error: archive special name unknown" 189 | msgstr "" 190 | 191 | #: ../lib/errors.h:68 192 | msgid "Format error: ELF header truncated" 193 | msgstr "" 194 | 195 | #: ../lib/errors.h:69 196 | msgid "Format error: program header table truncated" 197 | msgstr "" 198 | 199 | #: ../lib/errors.h:70 200 | msgid "Format error: section header table truncated" 201 | msgstr "" 202 | 203 | #: ../lib/errors.h:71 204 | msgid "Format error: data region truncated" 205 | msgstr "" 206 | 207 | #: ../lib/errors.h:72 208 | msgid "Format error: program header table alignment" 209 | msgstr "" 210 | 211 | #: ../lib/errors.h:73 212 | msgid "Format error: section header table alignment" 213 | msgstr "" 214 | 215 | #: ../lib/errors.h:74 216 | msgid "Format error: bad parameter in Verdef record" 217 | msgstr "" 218 | 219 | #: ../lib/errors.h:75 220 | msgid "Format error: unknown Verdef version" 221 | msgstr "" 222 | 223 | #: ../lib/errors.h:76 224 | msgid "Format error: bad parameter in Verneed record" 225 | msgstr "" 226 | 227 | #: ../lib/errors.h:77 228 | msgid "Format error: unknown Verneed version" 229 | msgstr "" 230 | 231 | #: ../lib/errors.h:78 232 | msgid "Format error: bad e_shnum value" 233 | msgstr "" 234 | 235 | #: ../lib/errors.h:79 236 | msgid "Format error: bad e_shentsize value" 237 | msgstr "" 238 | 239 | #: ../lib/errors.h:80 240 | msgid "Format error: bad e_phentsize value" 241 | msgstr "" 242 | 243 | #: ../lib/errors.h:81 244 | msgid "Format error: unterminated string in string table" 245 | msgstr "" 246 | 247 | #: ../lib/errors.h:82 248 | msgid "Layout error: section size too small for data" 249 | msgstr "" 250 | 251 | #: ../lib/errors.h:83 252 | msgid "Layout error: overlapping sections" 253 | msgstr "" 254 | 255 | #: ../lib/errors.h:84 256 | msgid "Memory error: elf descriptor" 257 | msgstr "" 258 | 259 | #: ../lib/errors.h:85 260 | msgid "Memory error: archive symbol table" 261 | msgstr "" 262 | 263 | #: ../lib/errors.h:86 264 | msgid "Memory error: archive member header" 265 | msgstr "" 266 | 267 | #: ../lib/errors.h:87 268 | msgid "Memory error: ELF header" 269 | msgstr "" 270 | 271 | #: ../lib/errors.h:88 272 | msgid "Memory error: program header table" 273 | msgstr "" 274 | 275 | #: ../lib/errors.h:89 276 | msgid "Memory error: section header table" 277 | msgstr "" 278 | 279 | #: ../lib/errors.h:90 280 | msgid "Memory error: section descriptor" 281 | msgstr "" 282 | 283 | #: ../lib/errors.h:91 284 | msgid "Memory error: section data" 285 | msgstr "" 286 | 287 | #: ../lib/errors.h:92 288 | msgid "Memory error: output file space" 289 | msgstr "" 290 | 291 | #: ../lib/errors.h:93 292 | msgid "Memory error: temporary buffer" 293 | msgstr "" 294 | 295 | #: ../lib/errors.h:94 296 | msgid "GElf error: value out of range" 297 | msgstr "" 298 | 299 | #: ../lib/errors.h:95 300 | msgid "GElf error: index out of range" 301 | msgstr "" 302 | 303 | #: ../lib/errors.h:96 304 | msgid "GElf error: type mismatch" 305 | msgstr "" 306 | 307 | #: ../lib/errors.h:97 308 | msgid "GElf error: not enough memory for GElf_Sym" 309 | msgstr "" 310 | 311 | #: ../lib/errors.h:98 312 | msgid "GElf error: not enough memory for GElf_Dyn" 313 | msgstr "" 314 | 315 | #: ../lib/errors.h:99 316 | msgid "GElf error: not enough memory for GElf_Rela" 317 | msgstr "" 318 | 319 | #: ../lib/errors.h:100 320 | msgid "GElf error: not enough memory for GElf_Rel" 321 | msgstr "" 322 | -------------------------------------------------------------------------------- /po/stamp-po: -------------------------------------------------------------------------------- 1 | timestamp 2 | -------------------------------------------------------------------------------- /stamp-h.in: -------------------------------------------------------------------------------- 1 | timestamp 2 | --------------------------------------------------------------------------------