Skip to content

Commit 71de612

Browse files
authored
[lld][MachO] Prevent doubled N_SO when comp_dir and name absolute (#71608)
When forming MachO STABS, this change detects if the DW_AT_name of the compile unit is already absolute (as allowed by DWARF), and if so, does not prepend DW_AT_comp_dir. Fixes #70995
1 parent 4a9c71b commit 71de612

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

lld/MachO/InputFiles.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1522,13 +1522,22 @@ void ObjFile::registerEhFrames(Section &ehFrameSection) {
15221522
}
15231523

15241524
std::string ObjFile::sourceFile() const {
1525+
const char *unitName = compileUnit->getUnitDIE().getShortName();
1526+
// DWARF allows DW_AT_name to be absolute, in which case nothing should be
1527+
// prepended. As for the styles, debug info can contain paths from any OS, not
1528+
// necessarily an OS we're currently running on. Moreover different
1529+
// compilation units can be compiled on different operating systems and linked
1530+
// together later.
1531+
if (sys::path::is_absolute(unitName, llvm::sys::path::Style::posix) ||
1532+
sys::path::is_absolute(unitName, llvm::sys::path::Style::windows))
1533+
return unitName;
15251534
SmallString<261> dir(compileUnit->getCompilationDir());
15261535
StringRef sep = sys::path::get_separator();
15271536
// We don't use `path::append` here because we want an empty `dir` to result
15281537
// in an absolute path. `append` would give us a relative path for that case.
15291538
if (!dir.endswith(sep))
15301539
dir += sep;
1531-
return (dir + compileUnit->getUnitDIE().getShortName()).str();
1540+
return (dir + unitName).str();
15321541
}
15331542

15341543
lld::DWARFCache *ObjFile::getDwarf() {

lld/test/MachO/stabs.s

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@
145145
# PIE-NEXT: segment section address type
146146
# PIE-EMPTY:
147147

148+
## Check that an absolute DW_AT_name does not have DW_AT_comp_dir prepended
149+
## when forming N_SO.
150+
# RUN: llvm-mc -filetype obj -triple=x86_64-apple-darwin %t/abs-path.s -o %t/abs-path.o
151+
# RUN: %lld %t/abs-path.o -o %t/test
152+
# RUN: (llvm-objdump --section-headers %t/test; dsymutil -s %t/test) | FileCheck %s --check-prefix=ABS-PATH
153+
# ABS-PATH: (N_SO ) 00 0000 0000000000000000 '/foo.cpp'
154+
148155
#--- test.s
149156

150157
## Make sure we don't create STABS entries for absolute symbols.
@@ -287,3 +294,53 @@ ltmp1:
287294
.globl _no_debug
288295
_no_debug:
289296
ret
297+
298+
#--- abs-path.s
299+
.text
300+
.globl _main
301+
_main:
302+
Lfunc_begin0:
303+
retq
304+
Lfunc_end0:
305+
306+
.section __DWARF,__debug_str,regular,debug
307+
.asciz "/foo.cpp" ## string offset=0
308+
.asciz "/tmp" ## string offset=9
309+
.section __DWARF,__debug_abbrev,regular,debug
310+
Lsection_abbrev:
311+
.byte 1 ## Abbreviation Code
312+
.byte 17 ## DW_TAG_compile_unit
313+
.byte 1 ## DW_CHILDREN_yes
314+
.byte 3 ## DW_AT_name
315+
.byte 14 ## DW_FORM_strp
316+
.byte 27 ## DW_AT_comp_dir
317+
.byte 14 ## DW_FORM_strp
318+
.byte 17 ## DW_AT_low_pc
319+
.byte 1 ## DW_FORM_addr
320+
.byte 18 ## DW_AT_high_pc
321+
.byte 6 ## DW_FORM_data4
322+
.byte 0 ## EOM(1)
323+
.byte 0 ## EOM(2)
324+
.byte 0 ## EOM(3)
325+
.section __DWARF,__debug_info,regular,debug
326+
.set Lset0, Ldebug_info_end0-Ldebug_info_start0 ## Length of Unit
327+
.long Lset0
328+
Ldebug_info_start0:
329+
.short 4 ## DWARF version number
330+
.set Lset1, Lsection_abbrev-Lsection_abbrev ## Offset Into Abbrev. Section
331+
.long Lset1
332+
.byte 8 ## Address Size (in bytes)
333+
.byte 1 ## Abbrev [1] 0xb:0x48 DW_TAG_compile_unit
334+
.long 0 ## DW_AT_name
335+
.long 9 ## DW_AT_comp_dir
336+
.quad Lfunc_begin0 ## DW_AT_low_pc
337+
.set Lset3, Lfunc_end0-Lfunc_begin0 ## DW_AT_high_pc
338+
.long Lset3
339+
.byte 0 ## End Of Children Mark
340+
Ldebug_info_end0:
341+
342+
.section __DWARF,__debug_aranges,regular,debug
343+
ltmp1:
344+
.byte 0
345+
346+
.subsections_via_symbols

0 commit comments

Comments
 (0)