|
8 | 8 |
|
9 | 9 | #include "llvm/Object/ELF.h"
|
10 | 10 | #include "llvm/BinaryFormat/ELF.h"
|
11 |
| -#include "llvm/Support/LEB128.h" |
| 11 | +#include "llvm/Support/DataExtractor.h" |
12 | 12 |
|
13 | 13 | using namespace llvm;
|
14 | 14 | using namespace object;
|
@@ -373,74 +373,62 @@ ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
|
373 | 373 | Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
|
374 | 374 | if (!ContentsOrErr)
|
375 | 375 | 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') |
380 | 379 | 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); |
392 | 382 |
|
393 |
| - uint64_t NumRelocs = ReadSLEB(); |
394 |
| - uint64_t Offset = ReadSLEB(); |
| 383 | + uint64_t NumRelocs = Data.getSLEB128(Cur); |
| 384 | + uint64_t Offset = Data.getSLEB128(Cur); |
395 | 385 | uint64_t Addend = 0;
|
396 | 386 |
|
397 |
| - if (ErrStr) |
398 |
| - return createError(ErrStr); |
| 387 | + if (!Cur) |
| 388 | + return std::move(Cur.takeError()); |
399 | 389 |
|
400 | 390 | std::vector<Elf_Rela> Relocs;
|
401 | 391 | Relocs.reserve(NumRelocs);
|
402 | 392 | while (NumRelocs) {
|
403 |
| - uint64_t NumRelocsInGroup = ReadSLEB(); |
| 393 | + uint64_t NumRelocsInGroup = Data.getSLEB128(Cur); |
| 394 | + if (!Cur) |
| 395 | + return std::move(Cur.takeError()); |
404 | 396 | if (NumRelocsInGroup > NumRelocs)
|
405 | 397 | return createError("relocation group unexpectedly large");
|
406 | 398 | NumRelocs -= NumRelocsInGroup;
|
407 | 399 |
|
408 |
| - uint64_t GroupFlags = ReadSLEB(); |
| 400 | + uint64_t GroupFlags = Data.getSLEB128(Cur); |
409 | 401 | bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
|
410 | 402 | bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
|
411 | 403 | bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
|
412 | 404 | bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
|
413 | 405 |
|
414 | 406 | uint64_t GroupOffsetDelta;
|
415 | 407 | if (GroupedByOffsetDelta)
|
416 |
| - GroupOffsetDelta = ReadSLEB(); |
| 408 | + GroupOffsetDelta = Data.getSLEB128(Cur); |
417 | 409 |
|
418 | 410 | uint64_t GroupRInfo;
|
419 | 411 | if (GroupedByInfo)
|
420 |
| - GroupRInfo = ReadSLEB(); |
| 412 | + GroupRInfo = Data.getSLEB128(Cur); |
421 | 413 |
|
422 | 414 | if (GroupedByAddend && GroupHasAddend)
|
423 |
| - Addend += ReadSLEB(); |
| 415 | + Addend += Data.getSLEB128(Cur); |
424 | 416 |
|
425 | 417 | if (!GroupHasAddend)
|
426 | 418 | Addend = 0;
|
427 | 419 |
|
428 |
| - for (uint64_t I = 0; I != NumRelocsInGroup; ++I) { |
| 420 | + for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) { |
429 | 421 | Elf_Rela R;
|
430 |
| - Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB(); |
| 422 | + Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur); |
431 | 423 | R.r_offset = Offset;
|
432 |
| - R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB(); |
| 424 | + R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur); |
433 | 425 | if (GroupHasAddend && !GroupedByAddend)
|
434 |
| - Addend += ReadSLEB(); |
| 426 | + Addend += Data.getSLEB128(Cur); |
435 | 427 | R.r_addend = Addend;
|
436 | 428 | Relocs.push_back(R);
|
437 |
| - |
438 |
| - if (ErrStr) |
439 |
| - return createError(ErrStr); |
440 | 429 | }
|
441 |
| - |
442 |
| - if (ErrStr) |
443 |
| - return createError(ErrStr); |
| 430 | + if (!Cur) |
| 431 | + return std::move(Cur.takeError()); |
444 | 432 | }
|
445 | 433 |
|
446 | 434 | return Relocs;
|
|
0 commit comments