85 Commits

Author SHA1 Message Date
John Paul Adrian Glaubitz 5c25da2843 Add initial support for HPPA (PA-RISC) (#793) 2025-07-24 20:21:45 +10:00
Fractal Fir(Michał Kostrubiec) 9a6b94c8ec Bare bones support for DEC Alpha (#790) 2025-07-20 11:43:20 +10:00
hev 8446af2292 Add LoongArch32 ELF basic support (#765) 2025-04-18 21:16:12 +10:00
sozud bc8ae3100d Basic SuperH support (#762) 2025-04-10 19:12:35 +10:00
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
Davide a87b65a2a9 Add Mips64 N32 ABI (#743) 2024-11-16 10:07:44 +10:00
knickish fb4446b56e Add Architecture::M68k (#742) 2024-11-08 15:37:05 +10:00
Denis Drakhnia 34f6dcef6a elf: add basic support for E2K arch (#727) 2024-09-10 15:01:33 +10:00
OndrikB 112417e182 write/coff: Set checksum for BSS section symbols (#718) 2024-08-12 09:43:44 +10:00
Philip Craig 3a396f3e2b write/elf: fix writing of strtab when symtab is empty (#710)
Previously, if the symtab was empty then we were writing a strtab
that had 0 bytes of data. This gives a linker error:
SHT_STRTAB string table section [index 4] is empty

Also, if there is a symtab then there must be a strtab,
otherwise the error is:
invalid sh_type for string table section [index 0]: expected SHT_STRTAB, but got SHT_NULL
2024-07-24 12:42:54 +10:00
bjorn3 2a297a5a57 write/macho: Reverse the order of emitting relocations (#702)
This prevents a crash of Apple's new linker.
2024-06-28 20:45:21 +10:00
John Paul Adrian Glaubitz d34d1bcfc9 Add Architecture::Sparc32Plus (#700)
This adds support for 32-bit SPARC using the V9 baseline (V8plus).
2024-06-27 16:30:10 +10:00
Philip Craig fd693f49d8 Add Architecture::Sparc (#699) 2024-06-26 12:23:57 +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 284975d12e read: do not return null sections or symbols in unified API (#679)
This affects ELF and XCOFF, including some of the lower level APIs too.
2024-05-06 15:47:22 +10:00
Philip Craig b77473f0a7 write/macho: ensure Mach-O subsections are not zero size (#676)
For Mach-O, `add_symbol_data` now ensures that the symbol size
is at least 1 when subsections via symbols are enabled.
This change was made to support linking with ld-prime. It is also
unclear how this previously managed to work with ld64.

`write::Object::add_subsection` no longer enables subsections
via symbols for Mach-O. Use `set_subsections_via_symbols` instead.
This change was made because Mach-O subsections are all or nothing,
so this decision must be made before any symbols are added.

`write::Object::add_subsection` no longer adds data to the subsection.
This change was made because it was done with `append_section_data`,
but this is often not the correct way to add data to the subsection.
Usually `add_symbol_data` is a better choice.
2024-05-04 12:58:42 +10:00
Philip Craig d6094add33 build/elf: fix sh_info for SHT_DYNSYM section
It's possible to have local symbols in the dynamic symbol table,
and sh_info should count these, the same as it already does
for SHT_SYMTAB.

Also be sure to exclude these from the GNU hash table.
2024-03-25 16:02:26 +10:00
Philip Craig bafb1c3d3e build/elf: fix sh_size for attribute sections 2024-03-25 16:02:26 +10:00
Philip Craig f3432e9807 elf: allow for absence of dynstr (#646)
.dynstr doesn't need to be present if there are no dynamic strings.

- handle section index 0 in read::elf::SectionTable::strings
- don't require .dynstr when writing if there are no strings
- don't print name of symbol index 0 in readobj example
2024-03-19 14:56:51 +10:00
Philip Craig 67d53c27f7 build/elf: improve handling of sh_offset for SHT_NOBITS when writing (#645) 2024-03-19 13:40:38 +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 eb58357f93 Delete format specific variants in RelocationKind
Replace uses of these variants with read::Relocation::flags
and write::Relocation::flags.

Additionally, for write::Relocation, move the kind/encoding/size
fields into RelocationFlags::Generic, since these are not
required when using format specific variants.
2023-12-24 13:24:44 +10:00
Philip Craig 0aa432b7e7 macho: add SubArchitecture::Arm64E (#614) 2023-12-23 13:34:33 +10:00