diff options
author | Fangrui Song <i@maskray.me> | 2023-05-10 09:36:58 -0700 |
---|---|---|
committer | Fangrui Song <i@maskray.me> | 2023-05-10 09:36:58 -0700 |
commit | 689715f335aeffc0e9583ac1b2a5629b6dd47876 (patch) | |
tree | 92ecb250acaed017ba787e819ae1383a1f6ceb63 /lld | |
parent | ae63d5be37fe2f2a73734e812c596d1a30b4016f (diff) | |
download | llvm-689715f335aeffc0e9583ac1b2a5629b6dd47876.tar.gz |
[Object] Fix handling of Elf_Nhdr with sh_addralign=8
The generic ABI says:
> Padding is present, if necessary, to ensure 8 or 4-byte alignment for the next note entry (depending on whether the file is a 64-bit or 32-bit object). Such padding is not included in descsz.
Our parsing code currently aligns n_namesz. Fix the bug by aligning the start
offset of the descriptor instead. This issue has been benign because the primary
uses of sh_addralign=8 notes are `.note.gnu.property`, where
`sizeof(Elf_Nhdr) + sizeof("GNU") = 16` (already aligned by 8).
In practice, many 64-bit systems incorrectly use sh_addralign=4 notes.
We can use sh_addralign (= p_align) to decide the descriptor padding.
Treat an alignment of 0 and 1 as 4. This approach matches modern GNU readelf
(since 2018).
We have a few tests incorrectly using sh_addralign=0. We may make our behavior
stricter after fixing these tests.
Linux kernel dumped core files use `p_align=0` notes, so we need to support the
case for compatibility.
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D150022
Diffstat (limited to 'lld')
-rw-r--r-- | lld/ELF/InputFiles.cpp | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index b8291fb4307b..311e2c9a9e54 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -885,12 +885,13 @@ template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) { while (!data.empty()) { // Read one NOTE record. auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data()); - if (data.size() < sizeof(Elf_Nhdr) || data.size() < nhdr->getSize()) + if (data.size() < sizeof(Elf_Nhdr) || + data.size() < nhdr->getSize(sec.addralign)) reportFatal(data.data(), "data is too short"); Elf_Note note(*nhdr); if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") { - data = data.slice(nhdr->getSize()); + data = data.slice(nhdr->getSize(sec.addralign)); continue; } @@ -899,7 +900,7 @@ template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) { : GNU_PROPERTY_X86_FEATURE_1_AND; // Read a body of a NOTE record, which consists of type-length-value fields. - ArrayRef<uint8_t> desc = note.getDesc(); + ArrayRef<uint8_t> desc = note.getDesc(sec.addralign); while (!desc.empty()) { const uint8_t *place = desc.data(); if (desc.size() < 8) @@ -924,7 +925,7 @@ template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) { } // Go to next NOTE record to look for more FEATURE_1_AND descriptions. - data = data.slice(nhdr->getSize()); + data = data.slice(nhdr->getSize(sec.addralign)); } return featuresSet; |