Skip to content

Commit 0d6d35e

Browse files
committed
[lld-macho] -section_rename should work on synthetic sections too
Previously, we only applied the renames to ConcatOutputSections. Reviewed By: #lld-macho, thakis Differential Revision: https://reviews.llvm.org/D105079
1 parent e7e71e9 commit 0d6d35e

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

lld/MachO/SyntheticSections.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ InStruct macho::in;
4747
std::vector<SyntheticSection *> macho::syntheticSections;
4848

4949
SyntheticSection::SyntheticSection(const char *segname, const char *name)
50-
: OutputSection(SyntheticKind, name), segname(segname) {
50+
: OutputSection(SyntheticKind, name) {
51+
std::tie(this->segname, this->name) = maybeRenameSection({segname, name});
5152
isec = make<ConcatInputSection>(segname, name);
5253
isec->parent = this;
5354
syntheticSections.push_back(this);

lld/MachO/SyntheticSections.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SyntheticSection : public OutputSection {
4646
return sec->kind() == SyntheticKind;
4747
}
4848

49-
const StringRef segname;
49+
StringRef segname;
5050
// This fake InputSection makes it easier for us to write code that applies
5151
// generically to both user inputs and synthetics.
5252
InputSection *isec;

lld/MachO/Writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ static void sortSegmentsAndSections() {
855855
}
856856
}
857857

858-
static NamePair maybeRenameSection(NamePair key) {
858+
NamePair macho::maybeRenameSection(NamePair key) {
859859
auto newNames = config->sectionRenameMap.find(key);
860860
if (newNames != config->sectionRenameMap.end())
861861
return newNames->second;

lld/MachO/Writer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef LLD_MACHO_WRITER_H
1010
#define LLD_MACHO_WRITER_H
1111

12+
#include "Config.h"
13+
1214
#include <cstdint>
1315

1416
namespace lld {
@@ -27,6 +29,8 @@ class LoadCommand {
2729

2830
template <class LP> void writeResult();
2931

32+
NamePair maybeRenameSection(NamePair key);
33+
3034
void createSyntheticSections();
3135

3236
// Add bindings for symbols that need weak or non-lazy bindings.

lld/test/MachO/rename.s

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,22 @@
3030
# RUN: %lld \
3131
# RUN: -rename_section __FROM_SECT __from_sect __TO_SECT __to_sect \
3232
# RUN: -rename_segment __FROM_SEG __TO_SEG \
33+
# RUN: -rename_section __TEXT __cstring __RODATA __cstring \
3334
# RUN: -o %t %t.o
3435
# RUN: llvm-objdump --macho --all-headers %t | FileCheck %s
3536

3637
# CHECK: {{^}}Section{{$}}
3738
# CHECK-NEXT: sectname __text
3839
# CHECK-NEXT: segname __TEXT
3940
# CHECK: {{^}}Section{{$}}
40-
# CHECK-NOT: sectname __from_sect
4141
# CHECK-NEXT: sectname __to_sect
42-
# CHECK-NOT: segname __FROM_SECT
4342
# CHECK-NEXT: segname __TO_SECT
4443
# CHECK: {{^}}Section{{$}}
4544
# CHECK-NEXT: sectname __from_seg
46-
# CHECK-NOT: segname __FROM_SEG
4745
# CHECK-NEXT: segname __TO_SEG
46+
# CHECK: {{^}}Section{{$}}
47+
# CHECK-NEXT: sectname __cstring
48+
# CHECK-NEXT: segname __RODATA
4849

4950
.section __FROM_SECT,__from_sect
5051
.global _from_sect
@@ -56,6 +57,10 @@ _from_sect:
5657
_from_seg:
5758
.space 8
5859

60+
## This is a synthetic section; make sure it gets renamed too.
61+
.cstring
62+
.space 8
63+
5964
.text
6065
.global _main
6166
_main:

0 commit comments

Comments
 (0)