Skip to content

[lld][MachO] Prevent doubled N_SO when comp_dir and name absolute #71608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 8, 2023

Conversation

mysterymath
Copy link
Contributor

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

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 llvm#70995
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2023

@llvm/pr-subscribers-lld-macho

@llvm/pr-subscribers-lld

Author: Daniel Thornburgh (mysterymath)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/71608.diff

2 Files Affected:

  • (modified) lld/MachO/InputFiles.cpp (+4-1)
  • (added) lld/test/MachO/stabs-abs-path.s (+58)
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 09c6ea9b19b5da2..4cf38169c4bfc9a 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -1522,13 +1522,16 @@ void ObjFile::registerEhFrames(Section &ehFrameSection) {
 }
 
 std::string ObjFile::sourceFile() const {
+  const char *unitName = compileUnit->getUnitDIE().getShortName();
+  if (sys::path::is_absolute(unitName))
+    return unitName;
   SmallString<261> dir(compileUnit->getCompilationDir());
   StringRef sep = sys::path::get_separator();
   // We don't use `path::append` here because we want an empty `dir` to result
   // in an absolute path. `append` would give us a relative path for that case.
   if (!dir.endswith(sep))
     dir += sep;
-  return (dir + compileUnit->getUnitDIE().getShortName()).str();
+  return (dir + unitName).str();
 }
 
 lld::DWARFCache *ObjFile::getDwarf() {
diff --git a/lld/test/MachO/stabs-abs-path.s b/lld/test/MachO/stabs-abs-path.s
new file mode 100644
index 000000000000000..565ed1ae7715598
--- /dev/null
+++ b/lld/test/MachO/stabs-abs-path.s
@@ -0,0 +1,58 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
+
+# RUN: %lld -lSystem %t.o -o %t
+# RUN: dsymutil -s %t | FileCheck %s
+
+# CHECK:      (N_SO         ) 00      0000   0000000000000000   '/foo.cpp'
+
+.text
+.globl  _main
+_main:
+Lfunc_begin0:
+  retq
+Lfunc_end0:
+
+.section  __DWARF,__debug_str,regular,debug
+  .asciz  "/foo.cpp"             ## string offset=0
+  .asciz  "/tmp"                 ## string offset=9
+.section  __DWARF,__debug_abbrev,regular,debug
+Lsection_abbrev:
+  .byte  1                       ## Abbreviation Code
+  .byte  17                      ## DW_TAG_compile_unit
+  .byte  1                       ## DW_CHILDREN_yes
+  .byte  3                       ## DW_AT_name
+  .byte  14                      ## DW_FORM_strp
+  .byte  27                      ## DW_AT_comp_dir
+  .byte  14                      ## DW_FORM_strp
+  .byte  17                      ## DW_AT_low_pc
+  .byte  1                       ## DW_FORM_addr
+  .byte  18                      ## DW_AT_high_pc
+  .byte  6                       ## DW_FORM_data4
+  .byte  0                       ## EOM(1)
+  .byte  0                       ## EOM(2)
+  .byte  0                       ## EOM(3)
+.section  __DWARF,__debug_info,regular,debug
+.set Lset0, Ldebug_info_end0-Ldebug_info_start0 ## Length of Unit
+  .long  Lset0
+Ldebug_info_start0:
+  .short  4                       ## DWARF version number
+.set Lset1, Lsection_abbrev-Lsection_abbrev ## Offset Into Abbrev. Section
+  .long  Lset1
+  .byte  8                       ## Address Size (in bytes)
+  .byte  1                       ## Abbrev [1] 0xb:0x48 DW_TAG_compile_unit
+  .long  0                       ## DW_AT_name
+  .long  9                       ## DW_AT_comp_dir
+  .quad  Lfunc_begin0            ## DW_AT_low_pc
+.set Lset3, Lfunc_end0-Lfunc_begin0     ## DW_AT_high_pc
+  .long  Lset3
+  .byte  0                       ## End Of Children Mark
+Ldebug_info_end0:
+
+.section  __DWARF,__debug_aranges,regular,debug
+ltmp1:
+  .byte 0
+
+.subsections_via_symbols
+
+

Copy link
Collaborator

@smeenai smeenai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, and thanks for fixing this! Will you need someone to commit this for you?

# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o

# RUN: %lld -lSystem %t.o -o %t
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-lSystem isn't needed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -0,0 +1,58 @@
# REQUIRES: x86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to create a new test file for this use case? This seems like this could be placed in stabs.s

Copy link
Contributor Author

@mysterymath mysterymath Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hesitant to try to add another file there given its complexity, but I'll give it a stab.

@mysterymath
Copy link
Contributor Author

LGTM, and thanks for fixing this! Will you need someone to commit this for you?

Nah, I've got it. Thanks though! :)

@mysterymath mysterymath merged commit 71de612 into llvm:main Nov 8, 2023
@mysterymath mysterymath deleted the macho-lld branch November 8, 2023 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LLD] ld64.lld adds doubled N_SO STABS entry when both DW_AT_comp_dir and DW_AT_name are absolute
4 participants