Commit Graph

216 Commits

Author SHA1 Message Date
Eelco Dolstra
5f37ae6b41 Factor out fetching ELF type 2016-09-19 17:50:13 +02:00
Eelco Dolstra
a365bcb7d7 Remove apparently incorrect usage of "static" 2016-09-19 17:31:37 +02:00
Eelco Dolstra
622b726b62 Some C++11 cleanup 2016-09-19 17:30:00 +02:00
Eelco Dolstra
b65ccb43b3 Cleanup: Remove "using namespace std" 2016-09-19 17:25:17 +02:00
Eelco Dolstra
3d28e1ed4d Use a std::vector for holding the file contents 2016-09-19 17:09:51 +02:00
Eelco Dolstra
8bc7b8c49e Remove tab 2016-09-19 16:17:54 +02:00
Tuomas Tynkkynen
4814341494 Add '--allowed-rpath-prefixes' option to '--shrink-rpath'
Fixes #97. In essence, the problem is that some packages in Nixpkgs have
RPATHs pointing to both $NIX_BUILD_TOP and $out, e.g.:

/tmp/nix-build-openldap-2.4.44.drv-0/openldap-2.4.44/libraries/libldap_r/.libs
/tmp/nix-build-openldap-2.4.44.drv-0/openldap-2.4.44/libraries/liblber/.libs
/nix/store/bfkmdxmv3a3f0g3d2q8jkdz2wam93c5z-openldap-2.4.44/lib
/nix/store/bfkmdxmv3a3f0g3d2q8jkdz2wam93c5z-openldap-2.4.44/lib64

Currently, running `patchelf --shrink-rpath` does the wrong thing by
keeping the /tmp/ paths and deleting the /nix/store ones. Now we can fix
the problem by using

patchelf --shrink-rpath --allowed-rpath-prefixes $NIX_STORE_DIR

in the Nixpkgs fixupPhase instead.
2016-06-04 00:25:02 +03:00
Tuomas Tynkkynen
2e3fdc2030 Extract a function for splitting a colon-separated string
We're going to need this logic in another place, so make a function of
this.
2016-06-03 23:03:51 +03:00
Eelco Dolstra
c66b2deb60 ` -> ' 2016-05-03 13:22:18 +02:00
Eelco Dolstra
0ed719370f Improve "cannot find section" error message
Issue #66
2016-05-03 13:20:58 +02:00
Nathaniel J. Smith
abae7858d6 Fix bug in walking .gnu.version_r linked list
When writing the code to teach --replace-needed to modify the
.gnu.version_r section (gh-85), I misunderstood how the ->vn_next
pointers in the Elf_Verneed structs are supposed to be interpreted: I
thought they gave an offset from the beginning of the section, but in
fact they give an offset relative to the current struct. The resulting
bug was very odd: generally, patchelf would complete without signalling
an error, but it would only successfully replace filenames that occurred
as either the first or second entries in the .gnu.version_r section,
while the third or later entries would be left untouched.

This commit fixes the interpretation of the ->vn_next pointers, so that
now --replace-needed should work correctly even on ELF files with more
than two version needed structs.

Thanks to @matthew-brett for finding the bug / providing a test case,
and to @rmcgibbo for helping me diagnose it.
2016-04-02 13:12:04 -07:00
Nathaniel J. Smith
907c02009e Small cleanups to replaceNeeded
No semantic changes, but I noticed some small errors in the DT_NEEDED
handling loop while I was adding the .gnu.version_r handling, so might
as well fix them while I'm here.
2016-04-01 00:12:38 -07:00
Nathaniel J. Smith
25cb97cc4f Teach --replace-needed to update .gnu.version_r table
If the ELF binary that we're patching is linked to a DSO that uses
symbol versioning, then the DSO's SONAME appears in two different
places: once as a DT_NEEDED entry, and once in the .gnu.version_r
version requirements section. Previously, patchelf --replace-needed
would update DT_NEEDED entry, but fail to update the .gnu.version_r
table. This resulted in completely broken binaries -- trying to load
them would trigger an assertion failure inside the dynamic loader, as it
tries to check the version of a library that was never loaded:

  Inconsistency detected by ld.so: dl-version.c: 224: _dl_check_map_versions: Assertion `needed != ((void *)0)' failed!

This commit teaches --replace-needed to update the .gnu.version_r
table.

Fixes: gh-84
2016-04-01 00:07:11 -07:00
Tuomas Tynkkynen
65a4dc6aa9 rewriteHeaders(): Don't assume PT_PHDR is the first program header
Because this assumption doesn't seem to be valid either in theory or
practice: the spec (http://refspecs.linuxbase.org/elf/elf.pdf) only
places these requirements on PT_PHDR:

"This segment type may not occur more than once in a file. Moreover,
it may occur only if the program header table is part of the memory
image of the program. If it is present, it must precede any loadable
segment entry."

And on ARM, binaries generated by GNU GCC / binutils almost never have
PT_PHDR as the first entry, e.g. the coreutils 'ls' has this:

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  EXIDX          0x0169b4 0x0001e9b4 0x0001e9b4 0x00008 0x00008 R   0x4
  PHDR           0x000034 0x00008034 0x00008034 0x00120 0x00120 R E 0x4
  INTERP         0x000154 0x00008154 0x00008154 0x0004f 0x0004f R   0x1
      [Requesting program interpreter: /nix/store/whcad4dnkp5pnhbv4p0f9k8srv0kmcjk-glibc-2.21/lib/ld-linux-armhf.so.3]
  LOAD           0x000000 0x00008000 0x00008000 0x169c0 0x169c0 R E 0x8000
  LOAD           0x0169c0 0x000269c0 0x000269c0 0x003f4 0x01088 RW  0x8000
  DYNAMIC        0x0169cc 0x000269cc 0x000269cc 0x000f8 0x000f8 RW  0x4
  NOTE           0x0001a4 0x000081a4 0x000081a4 0x00020 0x00020 R   0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4
  PAX_FLAGS      0x000000 0x00000000 0x00000000 0x00000 0x00000     0x4

This problem has existed for quite a long time on ARM and often results
in patchelf'd programs segfaulting inside the glibc dynamic linker,
which relies on PT_PHDR containing valid contents. This has been worked
around in Nixpkgs in various creative ways, like:

5c20877d40/nixos/modules/system/boot/stage-1.nix (L109)

Applying patchelf twice did actually work in practice due to the fact
that patchelf sorts the program headers, causing the first round
of patchelf to rewrite an invalid PT_PHDR to appear first, and then
the second round of patchelf fixing that PT_PHDR.
2016-02-22 20:34:09 +01:00
Eelco Dolstra
501009735d Merge branch 'pagesize' of https://github.com/fsateler/patchelf 2016-02-22 20:08:22 +01:00
darealshinji
0fa104d5ce Add a license header
Because why not?
2016-02-22 20:06:53 +01:00
Felipe Sateler
20ccc36b9c Allow overriding pagesize at runtime via a command-line argument 2016-01-14 22:57:28 -03:00
Felipe Sateler
5c5026e572 Allow specifying page size at configure time 2016-01-14 22:47:00 -03:00
Eelco Dolstra
dd299dc2a7 Don't use dynamic page size
This mostly reverts 08050dd5e3 (#54)
because the page size of the host is not necessarily the same as the
page size of the binary. For a proper fix, we'll need some way to
determine the page size of the binary, but ELF doesn't readily provide
this information.

Fixes #69.
2016-01-08 16:12:01 +01:00
Eelco Dolstra
1d0ab307db Merge branch 'print-needed' of https://github.com/darealshinji/patchelf 2016-01-08 14:48:15 +01:00
Eelco Dolstra
71afe7327f Merge branch 'patch-2' of https://github.com/darealshinji/patchelf 2016-01-08 14:42:34 +01:00
Eelco Dolstra
c361e32940 Merge branch 'patch-3' of https://github.com/darealshinji/patchelf 2016-01-08 14:16:00 +01:00
darealshinji
49974076ba Remaining sing-compare warnings
Get rid of remaining sing-compare warnings that appear when using gcc 5.2.
2015-11-29 20:14:16 +01:00
darealshinji
991168c1ff Remove unused parameter `fileMode'
I forgot to do that with this commit: 8eb28c00b6
Warnings only appear if -Wextra is turned on.
2015-07-24 21:17:46 +02:00
darealshinji
837f36efb0 Description of `--set-soname' command
`--set-soname` now creates a new DT_SONAME entry if it doesn't exist.
2015-07-24 20:37:12 +02:00
darealshinji
34e50f2e1d Add --print-needed command to list all DT_NEEDED entries of a file 2015-07-24 18:49:12 +02:00
Eelco Dolstra
e03e1644dc Merge branch 'skip-missing' of https://github.com/ttuegel/patchelf 2015-07-24 01:34:24 +02:00
Eelco Dolstra
884413a712 Merge branch 'move-sections' of https://github.com/ttuegel/patchelf 2015-07-24 01:33:34 +02:00
Eelco Dolstra
719b4aa4c8 Merge branch 'patch-3' of https://github.com/darealshinji/patchelfmod 2015-07-23 23:38:11 +02:00
Eelco Dolstra
a0c441999e Merge branch 'patch-2' of https://github.com/darealshinji/patchelfmod 2015-07-23 23:36:51 +02:00
Eelco Dolstra
6900cf904c Merge branch 'patch-1' of https://github.com/darealshinji/patchelfmod
Conflicts:
	configure.ac
2015-07-23 23:33:27 +02:00
Adrien Devresse
35ecfa7b62 Configure automatically the page size with a runtime call, solve the issue #53 related to prtability on powerpc64 architecture 2015-07-23 23:31:22 +02:00
darealshinji
36a33fbffb Add '--remove-rpath' option 2015-07-13 20:09:24 +02:00
darealshinji
764dacc6d6 Create new soname entry if shlib is missing one, merge modifySoname() and getSoname()
Basically this is just a modified copy of the rpath code
2015-07-13 19:52:35 +02:00
darealshinji
8eb28c00b6 Don't create a temporary copy to not break hardlinks (fixes #32) 2015-07-13 18:54:57 +02:00
Eelco Dolstra
f6886c2c33 Quick fix for #47 2015-07-10 18:12:37 +02:00
Eelco Dolstra
c90e0381dd Merge pull request #35 from darealshinji/patch-2
Update elf.h
2015-06-23 13:42:18 +02:00
Eelco Dolstra
f8a9ded798 Shut up some warnings 2014-11-06 16:55:35 +01:00
darealshinji
89b04c2275 let's print compiler warnings 2014-11-06 16:50:15 +01:00
Changli Gao
c09a356552 Add option --no-default-lib
Marks the object that the search for dependencies of this object will ignore any
default library search paths.
2014-10-23 01:41:47 +08:00
Thomas Tuegel
4e8c47fa84 Move section headers if they would be overwritten
When rewriting the sections of an executable, it can happen that the
section headers occur too early in the file and would be overwritten by
the replaced sections. If this would happen, we move the section headers
to the end of the file.
2014-09-12 15:24:35 -05:00
Thomas Tuegel
45fc82678d Allow DT_REL without .rel.dyn or .rel.got
Some DT_REL files are missing both .rel.dyn and .rel.got. This doesn't
seem to be an error, so it shouldn't be treated as such.
2014-09-12 15:24:15 -05:00
darealshinji
a3243f54f8 comment out Glibc-specific lines 2014-08-29 17:26:25 +02:00
darealshinji
eb76b51d8e Update elf.h 2014-08-28 19:22:47 +02:00
Jeremy Sanders
92fbe1a0e5 Use libattr and libacl to copy permissions if available 2014-07-29 02:46:07 +02:00
Eelco Dolstra
faf194fb46 Merge branch 'master' of github.com:ext/patchelf 2014-04-24 16:33:12 +02:00
Eelco Dolstra
7b515b5596 Merge branch 'master' of github.com:rgcjonas/patchelf 2014-04-24 16:32:05 +02:00
Chingis Dugarzhapov
dcc678e7c5 Clear help message for --print-soname and --set-soname options
Also fixed bad SONAME positioning in help output
2014-04-19 10:52:16 +00:00
Chingis Dugarzhapov
211ee2c00a Added assertion in TODO, removed some useless debug msgs... 2014-04-10 18:52:50 +02:00
Chingis Dugarzhapov
b02d21f9b3 raise error if no DT_SONAME found 2014-04-10 18:37:50 +02:00