aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDima Stepanov <dstepanov.src@gmail.com>2016-05-19 18:15:54 +0000
committerDima Stepanov <dstepanov.src@gmail.com>2016-05-19 18:15:54 +0000
commit78f132bf693a631e02a91adcc6584859a7cbd87f (patch)
tree6315356a47e1c6adb4b1215304b16d325b0cb1fa
parenta06bcd0ae8637dbb02878cef487131e4c4e86acf (diff)
Fix the function to set the section VMA/LMA fields in case of using
the linker script. The cycle in the ELF/LinkerScript.cpp:assignAddresses() routine will be used to go through all the sections and set all the addresses correctly. Add new test to check this case. git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@270090 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--ELF/LinkerScript.cpp45
-rw-r--r--test/ELF/linkerscript-repsection-va.s24
2 files changed, 44 insertions, 25 deletions
diff --git a/ELF/LinkerScript.cpp b/ELF/LinkerScript.cpp
index da8a19370..5387b026c 100644
--- a/ELF/LinkerScript.cpp
+++ b/ELF/LinkerScript.cpp
@@ -207,15 +207,6 @@ bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
}
template <class ELFT>
-static OutputSectionBase<ELFT> *
-findSection(ArrayRef<OutputSectionBase<ELFT> *> V, StringRef Name) {
- for (OutputSectionBase<ELFT> *Sec : V)
- if (Sec->getName() == Name)
- return Sec;
- return nullptr;
-}
-
-template <class ELFT>
void LinkerScript<ELFT>::assignAddresses(
ArrayRef<OutputSectionBase<ELFT> *> Sections) {
// Orphan sections are sections present in the input files which
@@ -239,23 +230,27 @@ void LinkerScript<ELFT>::assignAddresses(
continue;
}
- OutputSectionBase<ELFT> *Sec = findSection<ELFT>(Sections, Cmd.SectionName);
- if (!Sec)
- continue;
-
- if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
- uintX_t TVA = Dot + ThreadBssOffset;
- TVA = alignTo(TVA, Sec->getAlign());
- Sec->setVA(TVA);
- ThreadBssOffset = TVA - Dot + Sec->getSize();
- continue;
- }
+ // Find all the sections with required name. There can be more than
+ // ont section with such name, if the alignment, flags or type
+ // attribute differs.
+ for (OutputSectionBase<ELFT> *Sec : Sections) {
+ if (Sec->getName() != Cmd.SectionName)
+ continue;
+
+ if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
+ uintX_t TVA = Dot + ThreadBssOffset;
+ TVA = alignTo(TVA, Sec->getAlign());
+ Sec->setVA(TVA);
+ ThreadBssOffset = TVA - Dot + Sec->getSize();
+ continue;
+ }
- if (Sec->getFlags() & SHF_ALLOC) {
- Dot = alignTo(Dot, Sec->getAlign());
- Sec->setVA(Dot);
- Dot += Sec->getSize();
- continue;
+ if (Sec->getFlags() & SHF_ALLOC) {
+ Dot = alignTo(Dot, Sec->getAlign());
+ Sec->setVA(Dot);
+ Dot += Sec->getSize();
+ continue;
+ }
}
}
}
diff --git a/test/ELF/linkerscript-repsection-va.s b/test/ELF/linkerscript-repsection-va.s
new file mode 100644
index 000000000..4feeaa0e1
--- /dev/null
+++ b/test/ELF/linkerscript-repsection-va.s
@@ -0,0 +1,24 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "SECTIONS {.foo : {*(.foo.*)} }" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-objdump -section-headers %t1 | FileCheck %s
+# CHECK: Sections:
+# CHECK-NEXT: Idx Name Size Address Type
+# CHECK-NEXT: 0 00000000 0000000000000000
+# CHECK-NEXT: 1 .foo 00000004 0000000000000158 DATA
+# CHECK-NEXT: 2 .foo 00000004 000000000000015c DATA
+# CHECK-NEXT: 3 .text 00000001 0000000000000160 TEXT DATA
+
+.global _start
+_start:
+ nop
+
+.section .foo.1,"a"
+foo1:
+ .long 0
+
+.section .foo.2,"aw"
+foo2:
+ .long 0