Commit Graph

73 Commits

Author SHA1 Message Date
Sylvestre Ledru
ad31adba4c Bug 1547143 - Format the tree: Be prescriptive with the pointer style (left) r=Ehsan
# ignore-this-changeset

Depends on D28954

Differential Revision: https://phabricator.services.mozilla.com/D28956
2019-05-01 08:47:10 +00:00
Nathan Froyd
2125578ae7 Bug 1542958 - fix code section adjustments in elfhack; r=glandium
We were never adjusting `last` in this loop, so we were computing the
wrong addresses for all sections beyond the first.  Which in turn meant
that we would compute the wrong size for the section data we needed to
allocate.

Differential Revision: https://phabricator.services.mozilla.com/D28195
2019-04-22 23:44:28 +00:00
Nathan Froyd
803bd53dd6 Bug 1542958 - avoid malloc/delete[] mismatches in elfhack; r=glandium
We were allocating ElfSection's data with `new[]` and modifying it with
`realloc` in some places, which causes allocator mismatches.
Consistently manage the data with `malloc`, `realloc`, and `free` instead.

Differential Revision: https://phabricator.services.mozilla.com/D27327
2019-04-15 23:40:04 +00:00
Mike Hommey
b8adabaea3 Bug 1525510 - Add a trampoline to call original_init when it's not possible directly. r=froydnj
Differential Revision: https://phabricator.services.mozilla.com/D18807
2019-02-07 16:23:49 +00:00
Ehsan Akhgari
498e2df734 Bug 1521000 - Part 2: Adjust our clang-format rules to include spaces after the hash for nested preprocessor directives r=sylvestre
# ignore-this-changeset
2019-01-18 10:16:18 +01:00
Sylvestre Ledru
e5a134f73a Bug 1511181 - Reformat everything to the Google coding style r=ehsan a=clang-format
# ignore-this-changeset
2018-11-30 11:46:48 +01:00
Mike Hommey
701d9de363 Bug 1499915 - Support undoing elfhack when the elfhacked sections are in separate segments r=froydnj
This is some sort of followup to bug 1423813, providing a minimalistic
way to undo elfhack when the elfhack sections are in separate segments,
which has been the case since bug 1385783 but didn't cause problems
on Android builds until bug 1423822.

Depends on D9622

Differential Revision: https://phabricator.services.mozilla.com/D9623
2018-10-24 13:42:38 +00:00
Mike Hommey
e253af8253 Bug 1499915 - Remove support for the elfhack filler segment r=froydnj
This effectively backs out bug 822584, which worked around a similar
problem to what we are facing with Android xpcshell, being that the
crash reporter doesn't handle the address space "fragmentation" induced
by elfhack. The work around worked, at the expense of some added
complexity.

It was used for B2G only, and has effectively been unused since B2G was
retired.

Differential Revision: https://phabricator.services.mozilla.com/D9622
2018-10-24 13:42:31 +00:00
Mike Hommey
e8964efedc Bug 1491140 - Count non-elfhacked relocations size when comparing to original size r=froydnj
When checking whether the new relocations sizes are going to be a win, we
need to account for the fact that there are non-elfhacked relocation
remaining.

Differential Revision: https://phabricator.services.mozilla.com/D5837
2018-09-14 13:16:25 +00:00
Mike Hommey
13ca51f943 Bug 1481861 - Move the eh_frame sections when they are before the first executable section. r=froydnj
In some rare cases, it is possible for one of the eh_frame sections'
original address to be larger than the address of the injected code
section, which is added before the first executable section. Namely,
this happens in the rare case where that eh_frame section is smaller
than the injected code section, and is adjacent to the first executable
section. We obviously want to move the eh_frame sections in that case,
since one of them is in the way.
2018-08-14 07:25:07 +09:00
Mike Hommey
3d37ec5577 Fix up for bug 1481727, because operator priorities are fucked up in C/C++. r=bustage on a CLOSED TREE 2018-08-09 07:07:14 +09:00
Mike Hommey
33ed0cd9df Bug 1481727 - Fix an elfhack corner case after bug 1423822. r=froydnj
Bug 1423822 moved the injected code section before the .text section.
When linking with lld, the text section is usually page aligned, and
starting a PT_LOAD. We inject code at the beginning of the PT_LOAD,
which means the PT_LOAD is going to be extended at least a page
downwards. And it means the preceding PT_LOAD can't finish in that same
page, so the overhead of the injected code is needs to account for the
page alignment.
2018-08-09 06:04:30 +09:00
Mike Hommey
c304456af4 Bug 1480688 - Don't try to move .eh_frame in case it's not necessary. r=froydnj
If the .eh_frame_hdr and .eh_frame sections are not between the elfhack
relocation and elfhack code sections, it's not going to change anything
to try to move it, so don't even try.

While here, adjust the adjacency test to error out when the section name
doesn't match, and account for the fact that the eh_frame_hdr section
might appear after eh_frame.
2018-08-03 16:39:50 +09:00
Andreea Pavel
bd3a560b27 Backed out changeset 6bfba6f64e7b (bug 1480688) for android build bustages on a CLOSED TREE 2018-08-04 12:53:55 +03:00
Mike Hommey
86a760eb75 Bug 1480688 - Don't try to move .eh_frame in case it's not necessary. r=froydnj
If the .eh_frame_hdr and .eh_frame sections are not between the elfhack
relocation and elfhack code sections, it's not going to change anything
to try to move it, so don't even try.

While here, adjust the adjacency test to error out when the section name
doesn't match.
2018-08-03 16:39:50 +09:00
Mike Hommey
43849b6e0c Bug 1480654 - Allow empty eh_frame entries. r=froydnj
Somehow, when building with LTO, clang can end up creating a eh_frame
section with only one, empty, entry (which just looks like a 4-bytes
long section full of 0x00).
2018-08-03 11:33:49 +09:00
Mike Hommey
70646f6473 Bug 1423822 - Allow to relocate eh_frame. r=froydnj 2018-08-01 09:29:09 +09:00
Mike Hommey
c2b7f4ac61 Bug 1423822 - Set the address for the elfhack code section based on that of the section it is attached to. r=froydnj
When linking with ld.bfd or gold, this changes the PT_LOAD in which the
elfhack code section ends up, making it go in the same one as .init, .text,
etc. rather than .rel.*. When linking with lld, this completely
avoids doing a PT_LOAD split, because lld already splits .rel.* and
.text.
2018-08-01 07:21:36 +09:00
Mike Hommey
efd6d6d7b4 Bug 1423822 - Handle more cases of pointer reuse in DT_INIT_ARRAY. r=froydnj 2018-07-31 16:13:35 +09:00
Mike Hommey
365bc5eea8 Bug 1423822 - Change how elfhack determines it's not worth trying. r=froydnj
The current check makes assumption wrt what PT_LOAD the injected sections
end up in, and won't work with upcoming changes.
2018-07-31 11:49:33 +09:00
Mike Hommey
38ee91af97 Bug 1423822 - Make elfhack -f work in all cases where no gain would happen. r=froydnj 2018-07-31 13:56:01 +09:00
Mike Hommey
0e0c6496e4 Bug 1470701 - Use run-time page size when changing mapping permissions in elfhack injected code. r=froydnj
When a binary has a PT_GNU_RELRO segment, the elfhack injected code
uses mprotect to add the writable flag to relocated pages before
applying relocations, removing it afterwards. To do so, the elfhack
program uses the location and size of the PT_GNU_RELRO segment, and
adjusts it to be aligned according to the PT_LOAD alignment.

The problem here is that the PT_LOAD alignment doesn't necessarily match
the actual page alignment, and the resulting mprotect may end up not
covering the full extent of what the dynamic linker has protected
read-only according to the PT_GNU_RELRO segment. In turn, this can lead
to a crash on startup when trying to apply relocations to the still
read-only locations.

Practically speaking, this doesn't end up being a problem on x86, where
the PT_LOAD alignment is usually 4096, which happens to be the page
size, but on Debian armhf, it is 64k, while the run time page size can be
4k.
2018-06-24 09:02:38 +09:00
Mike Hommey
83f6c6295f Bug 1440037 - Add support for R_X86_64_PLT32 relocations in elfhack. r=froydnj 2018-02-22 07:15:23 +09:00
Jean-Luc Bonnafoux
af40c367ad Bug 1428629 - elfhack.cpp prefer prefix ++ operator for non primitive types r=froydnj
MozReview-Commit-ID: C0L2NUsbmc4
2018-01-08 09:30:32 +01:00
Mike Hommey
3a59e2eb56 Bug 1423813 - Properly handle elfhack -r after bug 1385783. r=froydnj
Bug 1385783 changed things such that the two elfhack sections are not
adjacent anymore. They can even be in different segments in some cases,
but the undo code doesn't know how to actually handle that case.

So for now, allow non adjacent sections, but still verify that they are
in the same segment.
2017-12-07 15:22:22 +09:00
Mike Hommey
ce1ab2fa9c Bug 1401005 - Handle the case where the relocation addend is not found at the relocation location. r=froydnj 2017-09-21 11:37:30 +09:00
Eugen Sawin
71d61acfcb Bug 1388893 - [1.0] Abort code insertion if executable section was not found. r=glandium 2017-08-15 13:58:41 +02:00
Mike Hommey
5061636ace Bug 1388713 - Change how elfhack looks for the bss section. r=froydnj
In bug 635961, elfhack was made to (ab)use the bss section as a
temporary space for a pointer. To find it, it scanned writable PT_LOAD
segments to find one that has a different file and memory size,
indicating the presence of .bss. This usually works fine, but when
the binary is linked with lld and relro is enabled, the end of the
file-backed part of the PT_LOAD segment containing the .bss section
ends up in the RELRO segment, making that location read-only and
subsequently making the elfhacked binary crash when it tries to restore
the .bss to a clean state, because it's not actually writing in the .bss
section: lld page aligns it after the RELRO segment.

So instead of scanning PT_LOAD segments, we scan for SHT_NOBITS
sections that are not SHF_TLS (i.e. not .tbss).
2017-08-10 07:55:55 +09:00
Mike Hommey
4dd5c54a20 Bug 1385783 - Insert the elfhack code before the first executable section. r=froydnj
The lld linker creates separate segments for purely executable sections
(such as .text) and sections preceding those (such as .rel.dyn). Neither
gold nor bfd ld do that, and just put all those sections in the same
executable segment.

Since elfhack is putting its executable code between the two relocation
sections, it ends up in a non-executable segment, leading to a crash
when it's time to run that code.

We thus insert the elfhack code before the first executable section
instead of between the two relocation sections (which is where the
elfhack data lies, and stays).
2017-08-02 16:39:12 +09:00
Mike Hommey
b09d245c16 Bug 1385783 - Don't assume both elfhack sections are next to each other. r=froydnj 2017-08-02 16:05:07 +09:00
Cameron McCormack
eb38b43907 Bug 1385537 - Check for writable segments correctly. r=glandium
MozReview-Commit-ID: FItpvVeiMJM
2017-07-29 13:56:25 +08:00
Mike Hommey
c0b84a749c Bug 635961 - Allow elfhack to relocate data under the GNU_RELRO segment. r=froydnj 2017-07-11 07:41:07 +09:00
Wes Kocher
cc389365c2 Backed out 2 changesets (bug 635961) at developer's request a=backout
Backed out changeset c56fa9c1eda0 (bug 635961)
Backed out changeset ddda63d5366e (bug 635961)

MozReview-Commit-ID: I6NxBctFn8e
2017-07-25 17:57:43 -07:00
Mike Hommey
63f270d06d Bug 1378986 - Avoid crashing in elfhack when the input file has no relocations. r=me a=bustage
MozReview-Commit-ID: 8jXvB8iRJkC
2017-07-25 15:50:34 -07:00
Mike Hommey
870d2795e2 Bug 635961 - Allow elfhack to relocate data under the GNU_RELRO segment. r=froydnj 2017-07-11 07:41:07 +09:00
Mike Hommey
8d189d4ddd Bug 1378986 - Avoid elfhack failing on weird DT_INIT_ARRAYs. r=froydnj
Somehow, with the Android toolchain, we end up with
non-empty-but-really-empty DT_INIT_ARRAYs.

In practical terms, they are arrays with no relocations, and content
that is meaningless:

  $ objdump -s -j .init_array libnss3.so

  libnss3.so:     file format elf32-little

  Contents of section .init_array:
   1086e0 00000000                             ....

  $ readelf -r libnss3.so | grep 1086e0

  $ objdump -s -j .init_array libplugin-container-pie.so

  libplugin-container-pie.so:     file format elf32-little

  Contents of section .init_array:
   4479c ffffffff 00000000 ffffffff 00000000  ................

  $ readelf -r libplugin-container-pie.so | grep 4479c

Because so far, elfhack expected meaningful DT_INIT_ARRAYs, it bailed out
early in that case.
2017-07-07 14:44:46 +09:00
Mike Hommey
b218c7ae91 Bug 982014 - Fix what sections are copied from injection object after bug 932737. r=nfroyd 2014-04-04 14:56:36 +09:00
Mike Hommey
eb31d1db37 Bug 940250 - Fix elfhack -r after bug 822584. r=nfroyd 2013-11-21 17:27:30 +09:00
Birunthan Mohanathas
c3c0be5691 Bug 784739 - Switch from NULL to nullptr in build/; r=ehsan 2013-11-11 14:13:38 -05:00
Mike Hommey
cb01e14b48 Bug 932737 - Change elfhack such that it doesn't require two injection objects. r=nfroyd 2013-11-02 08:47:24 +09:00
Mike Hommey
a92de2ca33 Bug 898998 - Turn BL into BLX when doing thumb call relocations and the target is ARM. r=nfroyd 2013-07-30 08:57:28 +09:00
Mike Hommey
86aa4f1773 Bug 892366 - Support R_ARM_CALL relocations in elfhack. r=nfroyd 2013-07-23 07:26:06 +09:00
Mike Hommey
08e7b0b9b7 Bug 892366 - Support R_ARM_JUMP24 relocations in elfhack. r=nfroyd 2013-07-12 09:14:20 +09:00
Mike Hommey
51f42ab769 Bug 851002 - Adjust Elf headers before adding filler segment in elfhack. r=nfroyd 2013-03-17 08:03:06 +01:00
Mike Hommey
943edeb1af Bug 822584 - Workaround in elfhack to accomodate for breakpad not handling the memory mapping induced by the elfhack/bionic linker combination. r=nfroyd 2013-01-02 10:17:32 +01:00
Mike Hommey
7c56034a36 Bug 816494 part 4 - Add a -r option to elfhack that re-merges the split PT_LOADs. r=nfroyd
Sections are positioned accordingly, which means the resulting ELF binary will
have a big gap full of zero between .rel.plt and .plt.
2012-12-10 10:33:08 +01:00
Mike Hommey
8b12e9c04c Bug 816494 part 3 - Allocate Elf instance on stack in do_file(). r=nfroyd 2012-12-10 10:33:08 +01:00
Mike Hommey
082d88f5c0 Bug 816494 part 2 - Move the PT_LOAD splitting logic in elfhack.cpp. r=nfroyd
Also, section offsets are not adjusted until the split is done.
2012-12-10 10:33:08 +01:00
Mike Hommey
f95e39436b Bug 716295 part 5 - Add support for R_ARM_THM_CALL relocations. r=nfroyd 2012-08-09 16:34:25 +02:00
Mike Hommey
aa4e4df82a Bug 716295 part 4 - Interpose elfhack injected code in DT_INIT_ARRAY's first entry when possible. r=nfroyd 2012-08-09 16:34:25 +02:00