Skip to content

Commit 3ca502a

Browse files
committed
Use DataExtractor to decode SLEB128 in android_relas.
A simple refactoring patch which let us use `DataExtractor::getSLEB128` rather than using a lambda function. Differential Revision: https://reviews.llvm.org/D95158
1 parent 374862d commit 3ca502a

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

llvm/lib/Object/ELF.cpp

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "llvm/Object/ELF.h"
1010
#include "llvm/BinaryFormat/ELF.h"
11-
#include "llvm/Support/LEB128.h"
11+
#include "llvm/Support/DataExtractor.h"
1212

1313
using namespace llvm;
1414
using namespace object;
@@ -373,74 +373,62 @@ ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
373373
Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
374374
if (!ContentsOrErr)
375375
return ContentsOrErr.takeError();
376-
const uint8_t *Cur = ContentsOrErr->begin();
377-
const uint8_t *End = ContentsOrErr->end();
378-
if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
379-
Cur[2] != 'S' || Cur[3] != '2')
376+
ArrayRef<uint8_t> Content = *ContentsOrErr;
377+
if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' ||
378+
Content[2] != 'S' || Content[3] != '2')
380379
return createError("invalid packed relocation header");
381-
Cur += 4;
382-
383-
const char *ErrStr = nullptr;
384-
auto ReadSLEB = [&]() -> int64_t {
385-
if (ErrStr)
386-
return 0;
387-
unsigned Len;
388-
int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
389-
Cur += Len;
390-
return Result;
391-
};
380+
DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
381+
DataExtractor::Cursor Cur(/*Offset=*/4);
392382

393-
uint64_t NumRelocs = ReadSLEB();
394-
uint64_t Offset = ReadSLEB();
383+
uint64_t NumRelocs = Data.getSLEB128(Cur);
384+
uint64_t Offset = Data.getSLEB128(Cur);
395385
uint64_t Addend = 0;
396386

397-
if (ErrStr)
398-
return createError(ErrStr);
387+
if (!Cur)
388+
return std::move(Cur.takeError());
399389

400390
std::vector<Elf_Rela> Relocs;
401391
Relocs.reserve(NumRelocs);
402392
while (NumRelocs) {
403-
uint64_t NumRelocsInGroup = ReadSLEB();
393+
uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
394+
if (!Cur)
395+
return std::move(Cur.takeError());
404396
if (NumRelocsInGroup > NumRelocs)
405397
return createError("relocation group unexpectedly large");
406398
NumRelocs -= NumRelocsInGroup;
407399

408-
uint64_t GroupFlags = ReadSLEB();
400+
uint64_t GroupFlags = Data.getSLEB128(Cur);
409401
bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
410402
bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
411403
bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
412404
bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
413405

414406
uint64_t GroupOffsetDelta;
415407
if (GroupedByOffsetDelta)
416-
GroupOffsetDelta = ReadSLEB();
408+
GroupOffsetDelta = Data.getSLEB128(Cur);
417409

418410
uint64_t GroupRInfo;
419411
if (GroupedByInfo)
420-
GroupRInfo = ReadSLEB();
412+
GroupRInfo = Data.getSLEB128(Cur);
421413

422414
if (GroupedByAddend && GroupHasAddend)
423-
Addend += ReadSLEB();
415+
Addend += Data.getSLEB128(Cur);
424416

425417
if (!GroupHasAddend)
426418
Addend = 0;
427419

428-
for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
420+
for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) {
429421
Elf_Rela R;
430-
Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
422+
Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
431423
R.r_offset = Offset;
432-
R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
424+
R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
433425
if (GroupHasAddend && !GroupedByAddend)
434-
Addend += ReadSLEB();
426+
Addend += Data.getSLEB128(Cur);
435427
R.r_addend = Addend;
436428
Relocs.push_back(R);
437-
438-
if (ErrStr)
439-
return createError(ErrStr);
440429
}
441-
442-
if (ErrStr)
443-
return createError(ErrStr);
430+
if (!Cur)
431+
return std::move(Cur.takeError());
444432
}
445433

446434
return Relocs;

llvm/test/tools/llvm-readobj/ELF/packed-relocs-errors.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
.section .rela.dyn, "a", @0x60000001
2424
.ascii "APS2"
2525

26-
# ERR-PAST-END: warning: '[[FILE]]': unable to read relocations from SHT_ANDROID_REL section with index 3: malformed sleb128, extends past end
26+
# ERR-PAST-END: warning: '[[FILE]]': unable to read relocations from SHT_ANDROID_REL section with index 3: unable to decode LEB128 at offset 0x{{([[:xdigit:]]{8})}}: malformed sleb128, extends past end
2727

2828
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %t/asm3.s -o %t3.o
2929
# RUN: llvm-readobj --relocations %t3.o 2>&1 | FileCheck %s -DFILE=%t3.o --check-prefix=ERR-PAST-END

0 commit comments

Comments
 (0)