Skip to content

Commit

Permalink
[mips] Put some MIPS-specific sections to separate segments
Browse files Browse the repository at this point in the history
Three MIPS-specific sections `.reginfo`, `.MIPS.options`, and `.MIPS.abiflags`
are used by loader to read their contents and setup environment for running
a program. Loader looks up these data in the corresponding segments:
`PT_MIPS_REGINFO`, `PT_MIPS_OPTIONS`, and `PT_MIPS_ABIFLAGS` respectively.

This patch put these sections to separate segments like we do already
for ARM `SHT_ARM_EXIDX` section.

Differential Revision: http://reviews.llvm.org/D58381

llvm-svn: 354468
  • Loading branch information
atanasyan committed Feb 20, 2019
1 parent 68171e3 commit 14b0981
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 23 deletions.
32 changes: 20 additions & 12 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ template <class ELFT> class Writer {

std::vector<PhdrEntry *> createPhdrs();
void removeEmptyPTLoad();
void addPtArmExid(std::vector<PhdrEntry *> &Phdrs);
void addPhdrForSection(std::vector<PhdrEntry *> &Phdrs, unsigned ShType,
unsigned PType, unsigned PFlags);
void assignFileOffsets();
void assignFileOffsetsBinary();
void setPhdrs();
Expand Down Expand Up @@ -1757,7 +1758,16 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// image base and the dynamic section on mips includes the image base.
if (!Config->Relocatable && !Config->OFormatBinary) {
Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs();
addPtArmExid(Phdrs);
if (Config->EMachine == EM_ARM) {
// PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
addPhdrForSection(Phdrs, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
}
if (Config->EMachine == EM_MIPS) {
// Add separate segments for MIPS-specific sections.
addPhdrForSection(Phdrs, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
addPhdrForSection(Phdrs, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
addPhdrForSection(Phdrs, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
}
Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();

// Find the TLS segment. This happens before the section layout loop so that
Expand Down Expand Up @@ -2053,19 +2063,17 @@ template <class ELFT> std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs() {
}

template <class ELFT>
void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry *> &Phdrs) {
if (Config->EMachine != EM_ARM)
return;
auto I = llvm::find_if(OutputSections, [](OutputSection *Cmd) {
return Cmd->Type == SHT_ARM_EXIDX;
});
void Writer<ELFT>::addPhdrForSection(std::vector<PhdrEntry *> &Phdrs,
unsigned ShType, unsigned PType,
unsigned PFlags) {
auto I = llvm::find_if(
OutputSections, [=](OutputSection *Cmd) { return Cmd->Type == ShType; });
if (I == OutputSections.end())
return;

// PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
PhdrEntry *ARMExidx = make<PhdrEntry>(PT_ARM_EXIDX, PF_R);
ARMExidx->add(*I);
Phdrs.push_back(ARMExidx);
PhdrEntry *Entry = make<PhdrEntry>(PType, PFlags);
Entry->add(*I);
Phdrs.push_back(Entry);
}

// The first section of each PT_LOAD, the first section in PT_GNU_RELRO and the
Expand Down
42 changes: 33 additions & 9 deletions lld/test/ELF/basic-mips.s
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ __start:
# CHECK-NEXT: ]
# CHECK-NEXT: HeaderSize: 52
# CHECK-NEXT: ProgramHeaderEntrySize: 32
# CHECK-NEXT: ProgramHeaderCount: 5
# CHECK-NEXT: ProgramHeaderCount: 7
# CHECK-NEXT: SectionHeaderEntrySize: 40
# CHECK-NEXT: SectionHeaderCount: 11
# CHECK-NEXT: StringTableSectionIndex: 9
Expand All @@ -61,8 +61,8 @@ __start:
# CHECK-NEXT: Flags [ (0x2)
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: ]
# CHECK-NEXT: Address: 0x100D8
# CHECK-NEXT: Offset: 0xD8
# CHECK-NEXT: Address: 0x10118
# CHECK-NEXT: Offset: 0x118
# CHECK-NEXT: Size: 24
# CHECK-NEXT: Link: 0
# CHECK-NEXT: Info: 0
Expand All @@ -76,8 +76,8 @@ __start:
# CHECK-NEXT: Flags [ (0x2)
# CHECK-NEXT: SHF_ALLOC (0x2)
# CHECK-NEXT: ]
# CHECK-NEXT: Address: 0x100F0
# CHECK-NEXT: Offset: 0xF0
# CHECK-NEXT: Address: 0x10130
# CHECK-NEXT: Offset: 0x130
# CHECK-NEXT: Size: 24
# CHECK-NEXT: Link: 0
# CHECK-NEXT: Info: 0
Expand Down Expand Up @@ -245,8 +245,8 @@ __start:
# CHECK-NEXT: Offset: 0x34
# CHECK-NEXT: VirtualAddress: 0x10034
# CHECK-NEXT: PhysicalAddress: 0x10034
# CHECK-NEXT: FileSize: 160
# CHECK-NEXT: MemSize: 160
# CHECK-NEXT: FileSize: 224
# CHECK-NEXT: MemSize: 224
# CHECK-NEXT: Flags [ (0x4)
# CHECK-NEXT: PF_R (0x4)
# CHECK-NEXT: ]
Expand All @@ -257,8 +257,8 @@ __start:
# CHECK-NEXT: Offset: 0x0
# CHECK-NEXT: VirtualAddress: 0x10000
# CHECK-NEXT: PhysicalAddress: 0x10000
# CHECK-NEXT: FileSize: 264
# CHECK-NEXT: MemSize: 264
# CHECK-NEXT: FileSize: 328
# CHECK-NEXT: MemSize: 328
# CHECK-NEXT: Flags [ (0x4)
# CHECK-NEXT: PF_R (0x4)
# CHECK-NEXT: ]
Expand Down Expand Up @@ -303,4 +303,28 @@ __start:
# CHECK-NEXT: ]
# CHECK-NEXT: Alignment: 0
# CHECK-NEXT: }
# CHECK-NEXT: ProgramHeader {
# CHECK-NEXT: Type: PT_MIPS_REGINFO
# CHECK-NEXT: Offset: 0x130
# CHECK-NEXT: VirtualAddress: 0x10130
# CHECK-NEXT: PhysicalAddress: 0x10130
# CHECK-NEXT: FileSize: 24
# CHECK-NEXT: MemSize: 24
# CHECK-NEXT: Flags [ (0x4)
# CHECK-NEXT: PF_R (0x4)
# CHECK-NEXT: ]
# CHECK-NEXT: Alignment: 4
# CHECK-NEXT: }
# CHECK-NEXT: ProgramHeader {
# CHECK-NEXT: Type: PT_MIPS_ABIFLAGS
# CHECK-NEXT: Offset: 0x118
# CHECK-NEXT: VirtualAddress: 0x10118
# CHECK-NEXT: PhysicalAddress: 0x10118
# CHECK-NEXT: FileSize: 24
# CHECK-NEXT: MemSize: 24
# CHECK-NEXT: Flags [ (0x4)
# CHECK-NEXT: PF_R (0x4)
# CHECK-NEXT: ]
# CHECK-NEXT: Alignment: 8
# CHECK-NEXT: }
# CHECK-NEXT: ]
2 changes: 1 addition & 1 deletion lld/test/ELF/mips-got-string.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# CHECK: Symbol {
# CHECK: Name: $.str
# CHECK-NEXT: Value: 0x1B1
# CHECK-NEXT: Value: 0x1F1
# CHECK: }

# CHECK: Local entries [
Expand Down
15 changes: 14 additions & 1 deletion lld/test/ELF/mips-options.s
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# RUN: . = 0x100000000; \
# RUN: .got : { *(.got) } }" > %t.rel.script
# RUN: ld.lld %t1.o %t2.o --gc-sections --script %t.rel.script -shared -o %t.so
# RUN: llvm-readobj -symbols -mips-options %t.so | FileCheck %s
# RUN: llvm-readobj -program-headers -symbols -mips-options %t.so | FileCheck %s

.text
.globl __start
Expand All @@ -18,6 +18,19 @@ __start:
# CHECK: Name: _gp
# CHECK-NEXT: Value: 0x[[GP:[0-9A-F]+]]

# CHECK: ProgramHeader {
# CHECK: Type: PT_MIPS_OPTIONS
# CHECK-NEXT: Offset:
# CHECK-NEXT: VirtualAddress:
# CHECK-NEXT: PhysicalAddress:
# CHECK-NEXT: FileSize:
# CHECK-NEXT: MemSize:
# CHECK-NEXT: Flags [
# CHECK-NEXT: PF_R
# CHECK-NEXT: ]
# CHECK-NEXT: Alignment: 8
# CHECK-NEXT: }

# CHECK: MIPS Options {
# CHECK-NEXT: ODK_REGINFO {
# CHECK-NEXT: GP: 0x[[GP]]
Expand Down

0 comments on commit 14b0981

Please sign in to comment.