llvm.org GIT mirror llvm / d784be6
[MC][ELF] compute entity size for explicit sections Summary: Global variables might declare themselves to be in explicit sections. Calculate the entity size always to prevent assembler warnings "entity size for SHF_MERGE not specified" when sections are to be marked merge-able. Fixes PR31828. Reviewers: rnk, echristo Reviewed By: rnk Subscribers: llvm-commits, pirama, srhines Differential Revision: https://reviews.llvm.org/D53056 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@344197 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Desaulniers 2 years ago
2 changed file(s) with 74 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
505505 return OtherGO ? dyn_cast(TM.getSymbol(OtherGO)) : nullptr;
506506 }
507507
508 MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
509 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
510 StringRef SectionName = GO->getSection();
511
512 // Check if '#pragma clang section' name is applicable.
513 // Note that pragma directive overrides -ffunction-section, -fdata-section
514 // and so section name is exactly as user specified and not uniqued.
515 const GlobalVariable *GV = dyn_cast(GO);
516 if (GV && GV->hasImplicitSection()) {
517 auto Attrs = GV->getAttributes();
518 if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
519 SectionName = Attrs.getAttribute("bss-section").getValueAsString();
520 } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
521 SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
522 } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
523 SectionName = Attrs.getAttribute("data-section").getValueAsString();
524 }
525 }
526 const Function *F = dyn_cast(GO);
527 if (F && F->hasFnAttribute("implicit-section-name")) {
528 SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
529 }
530
531 // Infer section flags from the section name if we can.
532 Kind = getELFKindForNamedSection(SectionName, Kind);
533
534 StringRef Group = "";
535 unsigned Flags = getELFSectionFlags(Kind);
536 if (const Comdat *C = getELFComdat(GO)) {
537 Group = C->getName();
538 Flags |= ELF::SHF_GROUP;
539 }
540
541 // A section can have at most one associated section. Put each global with
542 // MD_associated in a unique section.
543 unsigned UniqueID = MCContext::GenericSectionID;
544 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
545 if (AssociatedSymbol) {
546 UniqueID = NextUniqueID++;
547 Flags |= ELF::SHF_LINK_ORDER;
548 }
549
550 MCSectionELF *Section = getContext().getELFSection(
551 SectionName, getELFSectionType(SectionName, Kind), Flags,
552 /*EntrySize=*/0, Group, UniqueID, AssociatedSymbol);
553 // Make sure that we did not get some other section with incompatible sh_link.
554 // This should not be possible due to UniqueID code above.
555 assert(Section->getAssociatedSymbol() == AssociatedSymbol &&
556 "Associated symbol mismatch between sections");
557 return Section;
558 }
559
560 /// Return the section prefix name used by options FunctionsSections and
561 /// DataSections.
562 static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
563 if (Kind.isText())
564 return ".text";
565 if (Kind.isReadOnly())
566 return ".rodata";
567 if (Kind.isBSS())
568 return ".bss";
569 if (Kind.isThreadData())
570 return ".tdata";
571 if (Kind.isThreadBSS())
572 return ".tbss";
573 if (Kind.isData())
574 return ".data";
575 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
576 return ".data.rel.ro";
577 }
578
579508 static unsigned getEntrySizeForKind(SectionKind Kind) {
580509 if (Kind.isMergeable1ByteCString())
581510 return 1;
598527 assert(!Kind.isMergeableConst() && "unknown data width");
599528 return 0;
600529 }
530 }
531
532 MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
533 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
534 StringRef SectionName = GO->getSection();
535
536 // Check if '#pragma clang section' name is applicable.
537 // Note that pragma directive overrides -ffunction-section, -fdata-section
538 // and so section name is exactly as user specified and not uniqued.
539 const GlobalVariable *GV = dyn_cast(GO);
540 if (GV && GV->hasImplicitSection()) {
541 auto Attrs = GV->getAttributes();
542 if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
543 SectionName = Attrs.getAttribute("bss-section").getValueAsString();
544 } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
545 SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
546 } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
547 SectionName = Attrs.getAttribute("data-section").getValueAsString();
548 }
549 }
550 const Function *F = dyn_cast(GO);
551 if (F && F->hasFnAttribute("implicit-section-name")) {
552 SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
553 }
554
555 // Infer section flags from the section name if we can.
556 Kind = getELFKindForNamedSection(SectionName, Kind);
557
558 StringRef Group = "";
559 unsigned Flags = getELFSectionFlags(Kind);
560 if (const Comdat *C = getELFComdat(GO)) {
561 Group = C->getName();
562 Flags |= ELF::SHF_GROUP;
563 }
564
565 // A section can have at most one associated section. Put each global with
566 // MD_associated in a unique section.
567 unsigned UniqueID = MCContext::GenericSectionID;
568 const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
569 if (AssociatedSymbol) {
570 UniqueID = NextUniqueID++;
571 Flags |= ELF::SHF_LINK_ORDER;
572 }
573
574 MCSectionELF *Section = getContext().getELFSection(
575 SectionName, getELFSectionType(SectionName, Kind), Flags,
576 getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol);
577 // Make sure that we did not get some other section with incompatible sh_link.
578 // This should not be possible due to UniqueID code above.
579 assert(Section->getAssociatedSymbol() == AssociatedSymbol &&
580 "Associated symbol mismatch between sections");
581 return Section;
582 }
583
584 /// Return the section prefix name used by options FunctionsSections and
585 /// DataSections.
586 static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
587 if (Kind.isText())
588 return ".text";
589 if (Kind.isReadOnly())
590 return ".rodata";
591 if (Kind.isBSS())
592 return ".bss";
593 if (Kind.isThreadData())
594 return ".tdata";
595 if (Kind.isThreadBSS())
596 return ".tbss";
597 if (Kind.isData())
598 return ".data";
599 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
600 return ".data.rel.ro";
601601 }
602602
603603 static MCSectionELF *selectELFSectionForGlobal(
0 ; RUN: llc < %s | FileCheck %s
1 @a = internal unnamed_addr constant [1 x [1 x i32]] zeroinitializer, section ".init.rodata", align 4
2 ; CHECK: .init.rodata,"aM",@progbits,4