Skip to content

Commit 236197e

Browse files
committed
[lld-macho] Implement -oso_prefix
https://bugs.llvm.org/show_bug.cgi?id=50229 Differential Revision: https://reviews.llvm.org/D112291
1 parent 89d55d3 commit 236197e

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

lld/MachO/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ struct Configuration {
174174

175175
bool zeroModTime = false;
176176

177+
llvm::StringRef osoPrefix;
178+
177179
llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; }
178180

179181
llvm::MachO::PlatformKind platform() const {

lld/MachO/Driver.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,29 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
10951095
depTracker =
10961096
make<DependencyTracker>(args.getLastArgValue(OPT_dependency_info));
10971097

1098+
config->osoPrefix = args.getLastArgValue(OPT_oso_prefix);
1099+
if (!config->osoPrefix.empty()) {
1100+
// Expand special characters, such as ".", "..", or "~", if present.
1101+
// Note: LD64 only expands "." and not other special characters.
1102+
// That seems silly to imitate so we will not try to follow it, but rather
1103+
// just use real_path() to do it.
1104+
1105+
// The max path length is 4096, in theory. However that seems quite long
1106+
// and seems unlikely that any one would want to strip everything from the
1107+
// path. Hence we've picked a reasonably large number here.
1108+
SmallString<1024> expanded;
1109+
if (!fs::real_path(config->osoPrefix, expanded,
1110+
/*expand_tilde=*/true)) {
1111+
// Note: LD64 expands "." to be `<current_dir>/`
1112+
// (ie., it has a slash suffix) whereas real_path() doesn't.
1113+
// So we have to append '/' to be consistent.
1114+
StringRef sep = sys::path::get_separator();
1115+
if (config->osoPrefix.equals(".") && !expanded.endswith(sep))
1116+
expanded += sep;
1117+
config->osoPrefix = saver.save(expanded.str());
1118+
}
1119+
}
1120+
10981121
// Must be set before any InputSections and Symbols are created.
10991122
config->deadStrip = args.hasArg(OPT_dead_strip);
11001123

lld/MachO/Options.td

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ def non_global_symbols_no_strip_list : Separate<["-"], "non_global_symbols_no_st
576576
def oso_prefix : Separate<["-"], "oso_prefix">,
577577
MetaVarName<"<path>">,
578578
HelpText<"Remove the prefix <path> from OSO symbols in the debug map">,
579-
Flags<[HelpHidden]>,
580579
Group<grp_symtab>;
581580
def add_ast_path : Separate<["-"], "add_ast_path">,
582581
MetaVarName<"<path>">,

lld/MachO/SyntheticSections.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,10 @@ void SymtabSection::emitObjectFileStab(ObjFile *file) {
860860
if (!file->archiveName.empty())
861861
path.append({"(", file->getName(), ")"});
862862

863-
stab.strx = stringTableSection.addString(saver.save(path.str()));
863+
StringRef adjustedPath = saver.save(path.str());
864+
adjustedPath.consume_front(config->osoPrefix);
865+
866+
stab.strx = stringTableSection.addString(adjustedPath);
864867
stab.desc = 1;
865868
stab.value = file->modTime;
866869
stabs.emplace_back(std::move(stab));

lld/test/MachO/stabs.s

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@
5050
# RUN: FileCheck %s -DDIR=%t -DFOO_PATH=%t/foo.o \
5151
# RUN: -D#TEST_TIME=0x10 -D#FOO_TIME=0x20
5252

53+
## Check that we emit relative path to object files in OSO entries
54+
## when -oso_prefix <path> is used.
55+
# RUN: cd %t && %lld -lSystem test.o foo.o no-debug.o -oso_prefix "%t" -o %t/test-rel
56+
# RUN: dsymutil -s %t/test-rel | grep 'N_OSO' | FileCheck %s -D#TEST_TIME=0x10 -D#FOO_TIME=0x20 --check-prefix=REL-PATH
57+
# RUN: cd %t && %lld -lSystem test.o foo.o no-debug.o -oso_prefix "." -o %t/test-rel-dot
58+
# RUN: dsymutil -s %t/test-rel-dot | grep 'N_OSO' | FileCheck %s -D#TEST_TIME=0x10 -D#FOO_TIME=0x20 --check-prefix=REL-DOT
59+
## Set HOME to %t (for ~ to expand to)
60+
# RUN: cd %t && env HOME=%t %lld -lSystem test.o foo.o no-debug.o -oso_prefix "~" -o %t/test-rel-tilde
61+
# RUN: dsymutil -s %t/test-rel-tilde | grep 'N_OSO' | FileCheck %s -D#TEST_TIME=0x10 -D#FOO_TIME=0x20 --check-prefix=REL-PATH
62+
63+
5364
# RUN: cd %t && %lld -lSystem test.o foo.a no-debug.o -o %t/test
5465
# RUN: (llvm-objdump --section-headers %t/test; dsymutil -s %t/test) | \
5566
# RUN: FileCheck %s -DDIR=%t -DFOO_PATH=%t/foo.a\(foo.o\) \
@@ -65,6 +76,8 @@
6576

6677
# CHECK: (N_SO ) 00 0000 0000000000000000 '/tmp/test.cpp'
6778
# CHECK-NEXT: (N_OSO ) 03 0001 [[#%.16x,TEST_TIME]] '[[DIR]]/test.o'
79+
# REL-PATH: (N_OSO ) 03 0001 [[#%.16x,TEST_TIME]] '/test.o'
80+
# REL-DOT: (N_OSO ) 03 0001 [[#%.16x,TEST_TIME]] 'test.o'
6881
# CHECK-NEXT: (N_STSYM ) [[#%.2d,MORE_DATA_ID + 1]] 0000 [[#%.16x,STATIC:]] '_static_var'
6982
# CHECK-NEXT: (N_FUN ) [[#%.2d,TEXT_ID + 1]] 0000 [[#%.16x,MAIN:]] '_main'
7083
# CHECK-NEXT: (N_FUN ) 00 0000 0000000000000006{{$}}
@@ -87,6 +100,8 @@
87100
# CHECK-NEXT: (N_SO ) 01 0000 0000000000000000{{$}}
88101
# CHECK-NEXT: (N_SO ) 00 0000 0000000000000000 '/foo.cpp'
89102
# CHECK-NEXT: (N_OSO ) 03 0001 [[#%.16x,FOO_TIME]] '[[FOO_PATH]]'
103+
# REL-PATH-NEXT: (N_OSO ) 03 0001 [[#%.16x,FOO_TIME]] '/foo.o'
104+
# REL-DOT-NEXT: (N_OSO ) 03 0001 [[#%.16x,FOO_TIME]] 'foo.o'
90105
# CHECK-NEXT: (N_FUN ) [[#%.2d,TEXT_ID + 1]] 0000 [[#%.16x,FOO:]] '_foo'
91106
# CHECK-NEXT: (N_FUN ) 00 0000 0000000000000001{{$}}
92107
# CHECK-NEXT: (N_SO ) 01 0000 0000000000000000{{$}}

0 commit comments

Comments
 (0)