7 Commits

Author SHA1 Message Date
Timo von Hartz e764a2d169 read_ref: allow zero size reads at any offset (#758)
Some ELF files do have segments with size zero, at offsets that are
invalid / out of bounds. This commit changes the default `ReadRef`
implementation for `&[u8]` to permit reads that are out of bounds, if
the requested length is zero, by returning `&[]`.

Examples of such files are debug files created with `objcopy --only-keep-debug`,
for example

`/usr/lib/debug/.build-id/bd/dd2eaf3326ffce6d173666b5f3e62a376e123a.debug`
in package `libgedit-gfls-1-0-dbgsym_0.2.1-2_arm64.deb`:

```
~ cp /usr/lib/debug/.build-id/bd/dd2eaf3326ffce6d173666b5f3e62a376e123a.debug /tmp/bad_file.elf
~ r2 /tmp/bad_file.elf
[0x0000027c]> iI~binsz
binsz    64184
[0x0000027c]> iSS
[Segments]

nth paddr        size vaddr        vsize perm type name
―――――――――――――――――――――――――――――――――――――――――――――――――――――――
0   0x00000000  0x27c 0x00000000  0x55e0 -r-x MAP  LOAD0
1   0x0000fab8    0x0 0x0001fab8   0x5c0 -rw- MAP  LOAD1
2   0x0000fab8    0x0 0x0001fb28   0x240 -rw- MAP  DYNAMIC
[...]
8   0x0000fab8    0x0 0x0001fab8   0x548 -r-- MAP  GNU_RELRO
```

This file has multiple segments starting at `0xfab8` (the end of the
file), with a physical size of `0`, while the file size is also `0xfab8`
(64184).

The current `ReadRef` implementation for `&[u8]` causes
`ProgramHeader::data` to fail for them, causing e.g. `ElfSegment::bytes`
to also fail.

While a caller could handle this error, or one could provide their own
type implementing `ReadRef` this way, I think it makes sense to do this
by default, since no data is *actually* being read out of bounds, but
with the current implementation we still try to index out of bounds and
then error.

Notably with this change this also matches the implementation of
`ReadRef` for `ReadCache` which already has a similar check implemented
for `read_bytes_at`.
2025-02-22 16:39:24 +10:00
ajwerner f54ea5532c read/macho: support Go's debug section compression (#697)
When using compression, debug sections in Mach-O produced by the go
compiler have a __zdebug_ section name prefix, and the section data has
the same format as GNU .zdebug_ compression for ELF.

Support these section names in `Object::section_by_name`, and support
the compressed section data in `ObjectSection::compressed_data`.

This commit extracts the GNU-style section compression logic from the
read::elf::section to a module underneath read, and then uses it also
in read::macho.
2024-06-25 12:48:33 +10:00
Philip Craig 7b61896648 Fix unused items when building with feature combinations (#637) 2024-02-26 11:23:04 +10:00
Philip Craig fb0d876a0b Fix clippy warnings (#636) 2024-02-23 17:43:09 +10:00
Guillaume Girol f1a0ec96a1 read: check that sizes are smaller than the file length in ReadCache (#630)
This avoids large allocations due to invalid sizes in corrupted files.
2024-02-11 21:30:06 +10:00
Philip Craig 01b60e68ff read: add COFF bigobj support (#502) 2023-04-03 15:19:06 +10:00
Philip Craig ce369d0074 Move coff test 2021-11-25 14:55:25 +10:00