Skip to content

Commit f0a687d

Browse files
mstorsjotru
authored andcommitted
[LLD] [COFF] Fix handling of comdat .drectve sections (#68116)
This can happen when manually emitting strings into .drectve sections with `__attribute__((section(".drectve")))`, which is a way to emulate `#pragma comment(linker, "...")` for mingw compilers, without requiring building with -fms-extensions. Normally, this doesn't generate any comdat, but if compiled with -fsanitize=address, this section does get turned into a comdat section. This fixes #67261. This issue can be seen as a regression; a change in the "lli" tool in 17.x triggers this case, if compiled with ASAN enabled, triggering this unsupported corner case in LLD. With this change, LLD can handle it. (cherry picked from commit 503bc5f)
1 parent 8a8ade4 commit f0a687d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lld/COFF/InputFiles.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ std::optional<Symbol *> ObjFile::createDefined(
661661
if (prevailing) {
662662
SectionChunk *c = readSection(sectionNumber, def, getName());
663663
sparseChunks[sectionNumber] = c;
664+
if (!c)
665+
return nullptr;
664666
c->sym = cast<DefinedRegular>(leader);
665667
c->selection = selection;
666668
cast<DefinedRegular>(leader)->data = &c->repl;

lld/test/COFF/comdat-drectve.s

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# REQUIRES: x86
2+
3+
# RUN: llvm-mc -triple=x86_64-windows-gnu %s -filetype=obj -o %t.obj
4+
5+
# RUN: lld-link %t.obj -out:%t.exe -debug:symtab -subsystem:console
6+
# RUN: llvm-readobj --coff-exports %t.exe | FileCheck %s
7+
8+
# CHECK: Name: exportedFunc
9+
10+
## This assembly snippet has been reduced from what Clang generates from
11+
## this C snippet, with -fsanitize=address. Normally, the .drectve
12+
## section would be a regular section - but when compiled with
13+
## -fsanitize=address, it becomes a comdat section.
14+
##
15+
# void exportedFunc(void) {}
16+
# void mainCRTStartup(void) {}
17+
# static __attribute__((section(".drectve"), used)) const char export_chkstk[] =
18+
# "-export:exportedFunc";
19+
20+
.text
21+
.globl exportedFunc
22+
exportedFunc:
23+
retq
24+
25+
.globl mainCRTStartup
26+
mainCRTStartup:
27+
retq
28+
29+
.section .drectve,"dr",one_only,export_chkstk
30+
export_chkstk:
31+
.asciz "-export:exportedFunc"

0 commit comments

Comments
 (0)