|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "InputFiles.h" |
| 10 | +#include "OutputSections.h" |
10 | 11 | #include "Symbols.h" |
11 | 12 | #include "SyntheticSections.h" |
12 | 13 | #include "Target.h" |
| 14 | +#include "Thunks.h" |
| 15 | +#include "lld/Common/ErrorHandler.h" |
| 16 | +#include "llvm/ADT/SmallVector.h" |
13 | 17 | #include "llvm/BinaryFormat/ELF.h" |
| 18 | +#include "llvm/Support/ELFAttributes.h" |
14 | 19 | #include "llvm/Support/Endian.h" |
| 20 | +#include "llvm/Support/HexagonAttributeParser.h" |
| 21 | +#include "llvm/Support/HexagonAttributes.h" |
| 22 | +#include "llvm/Support/LEB128.h" |
15 | 23 |
|
16 | 24 | using namespace llvm; |
17 | 25 | using namespace llvm::object; |
@@ -415,4 +423,117 @@ int64_t Hexagon::getImplicitAddend(const uint8_t *buf, RelType type) const { |
415 | 423 | } |
416 | 424 | } |
417 | 425 |
|
| 426 | +namespace { |
| 427 | +class HexagonAttributesSection final : public SyntheticSection { |
| 428 | +public: |
| 429 | + HexagonAttributesSection(Ctx &ctx) |
| 430 | + : SyntheticSection(ctx, ".hexagon.attributes", SHT_HEXAGON_ATTRIBUTES, 0, |
| 431 | + 1) {} |
| 432 | + |
| 433 | + size_t getSize() const override { return size; } |
| 434 | + void writeTo(uint8_t *buf) override; |
| 435 | + |
| 436 | + static constexpr StringRef vendor = "hexagon"; |
| 437 | + DenseMap<unsigned, unsigned> intAttr; |
| 438 | + size_t size = 0; |
| 439 | +}; |
| 440 | +} // namespace |
| 441 | + |
| 442 | +static HexagonAttributesSection * |
| 443 | +mergeAttributesSection(Ctx &ctx, |
| 444 | + const SmallVector<InputSectionBase *, 0> §ions) { |
| 445 | + ctx.in.hexagonAttributes = std::make_unique<HexagonAttributesSection>(ctx); |
| 446 | + auto &merged = |
| 447 | + static_cast<HexagonAttributesSection &>(*ctx.in.hexagonAttributes); |
| 448 | + |
| 449 | + // Collect all tags values from attributes section. |
| 450 | + const auto &attributesTags = HexagonAttrs::getHexagonAttributeTags(); |
| 451 | + for (const InputSectionBase *sec : sections) { |
| 452 | + HexagonAttributeParser parser; |
| 453 | + if (Error e = parser.parse(sec->content(), llvm::endianness::little)) |
| 454 | + Warn(ctx) << sec << ": " << std::move(e); |
| 455 | + for (const auto &tag : attributesTags) { |
| 456 | + switch (HexagonAttrs::AttrType(tag.attr)) { |
| 457 | + case HexagonAttrs::ARCH: |
| 458 | + case HexagonAttrs::HVXARCH: |
| 459 | + if (auto i = parser.getAttributeValue(tag.attr)) { |
| 460 | + auto r = merged.intAttr.try_emplace(tag.attr, *i); |
| 461 | + if (!r.second && r.first->second != *i) { |
| 462 | + if (r.first->second < *i) |
| 463 | + r.first->second = *i; |
| 464 | + } |
| 465 | + } |
| 466 | + continue; |
| 467 | + |
| 468 | + case HexagonAttrs::HVXIEEEFP: |
| 469 | + case HexagonAttrs::HVXQFLOAT: |
| 470 | + case HexagonAttrs::ZREG: |
| 471 | + case HexagonAttrs::AUDIO: |
| 472 | + case HexagonAttrs::CABAC: |
| 473 | + if (auto i = parser.getAttributeValue(tag.attr)) { |
| 474 | + auto r = merged.intAttr.try_emplace(tag.attr, *i); |
| 475 | + if (!r.second && r.first->second != *i) { |
| 476 | + r.first->second |= *i; |
| 477 | + } |
| 478 | + } |
| 479 | + continue; |
| 480 | + } |
| 481 | + } |
| 482 | + } |
| 483 | + |
| 484 | + // The total size of headers: format-version [ <section-length> "vendor-name" |
| 485 | + // [ <file-tag> <size>. |
| 486 | + size_t size = 5 + merged.vendor.size() + 1 + 5; |
| 487 | + for (auto &attr : merged.intAttr) |
| 488 | + if (attr.second != 0) |
| 489 | + size += getULEB128Size(attr.first) + getULEB128Size(attr.second); |
| 490 | + merged.size = size; |
| 491 | + return &merged; |
| 492 | +} |
| 493 | + |
| 494 | +void HexagonAttributesSection::writeTo(uint8_t *buf) { |
| 495 | + const size_t size = getSize(); |
| 496 | + uint8_t *const end = buf + size; |
| 497 | + *buf = ELFAttrs::Format_Version; |
| 498 | + write32(ctx, buf + 1, size - 1); |
| 499 | + buf += 5; |
| 500 | + |
| 501 | + memcpy(buf, vendor.data(), vendor.size()); |
| 502 | + buf += vendor.size() + 1; |
| 503 | + |
| 504 | + *buf = ELFAttrs::File; |
| 505 | + write32(ctx, buf + 1, end - buf); |
| 506 | + buf += 5; |
| 507 | + |
| 508 | + for (auto &attr : intAttr) { |
| 509 | + if (attr.second == 0) |
| 510 | + continue; |
| 511 | + buf += encodeULEB128(attr.first, buf); |
| 512 | + buf += encodeULEB128(attr.second, buf); |
| 513 | + } |
| 514 | +} |
| 515 | + |
| 516 | +void elf::mergeHexagonAttributesSections(Ctx &ctx) { |
| 517 | + // Find the first input SHT_HEXAGON_ATTRIBUTES; return if not found. |
| 518 | + size_t place = |
| 519 | + llvm::find_if(ctx.inputSections, |
| 520 | + [](auto *s) { return s->type == SHT_HEXAGON_ATTRIBUTES; }) - |
| 521 | + ctx.inputSections.begin(); |
| 522 | + if (place == ctx.inputSections.size()) |
| 523 | + return; |
| 524 | + |
| 525 | + // Extract all SHT_HEXAGON_ATTRIBUTES sections into `sections`. |
| 526 | + SmallVector<InputSectionBase *, 0> sections; |
| 527 | + llvm::erase_if(ctx.inputSections, [&](InputSectionBase *s) { |
| 528 | + if (s->type != SHT_HEXAGON_ATTRIBUTES) |
| 529 | + return false; |
| 530 | + sections.push_back(s); |
| 531 | + return true; |
| 532 | + }); |
| 533 | + |
| 534 | + // Add the merged section. |
| 535 | + ctx.inputSections.insert(ctx.inputSections.begin() + place, |
| 536 | + mergeAttributesSection(ctx, sections)); |
| 537 | +} |
| 538 | + |
418 | 539 | void elf::setHexagonTargetInfo(Ctx &ctx) { ctx.target.reset(new Hexagon(ctx)); } |
0 commit comments