Skip to content

Commit 2c40135

Browse files
MaskRaygithub-actions[bot]
authored andcommitted
Automerge: MC: Support quoted symbol names
gas has supported " quoted symbols since 2015. Both \ and " need to be escaped. https://sourceware.org/pipermail/binutils/2015-August/090003.html We don't unescape \\ or \" in assembly strings, leading to clang -c --save-temps vs clang -c difference for the following C code: ``` int x asm("a\"\\b"); ``` Fix #138390 MC/COFF/safeseh.h looks incorrect. \01 in `.safeseh "\01foo"` is not a correct escape sequence. Change it to \\ Pull Request: llvm/llvm-project#138817
2 parents c354c10 + 8893d40 commit 2c40135

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed

bolt/test/runtime/X86/fdata-escape-chars.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ define internal void @static_symb_backslash_b() #0 {
8282
; INSTR_CHECK: Binary Function "main"
8383
; INSTR_CHECK: Exec Count : 1
8484
; INSTR_CHECK: {{([[:xdigit:]]+)}}: callq "symb whitespace" # Count: 1
85-
; INSTR_CHECK: {{([[:xdigit:]]+)}}: callq "symb backslash\" # Count: 2
85+
; INSTR_CHECK: {{([[:xdigit:]]+)}}: callq "symb backslash\\" # Count: 2
8686
; INSTR_CHECK: Binary Function "static symb backslash\/1(*2)"
8787
; INSTR_CHECK: Exec Count : 1
8888
; INSTR_CHECK: {{([[:xdigit:]]+)}}: callq "symb whitespace" # Count: 1

llvm/lib/MC/MCContext.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,27 @@ MCDataFragment *MCContext::allocInitialFragment(MCSection &Sec) {
212212
MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
213213
SmallString<128> NameSV;
214214
StringRef NameRef = Name.toStringRef(NameSV);
215+
if (NameRef.contains('\\')) {
216+
NameSV = NameRef;
217+
size_t S = 0;
218+
// Support escaped \\ and \" as in GNU Assembler. GAS issues a warning for
219+
// other characters following \\, which we do not implement due to code
220+
// structure.
221+
for (size_t I = 0, E = NameSV.size(); I != E; ++I) {
222+
char C = NameSV[I];
223+
if (C == '\\' && I + 1 != E) {
224+
switch (NameSV[I + 1]) {
225+
case '"':
226+
case '\\':
227+
C = NameSV[++I];
228+
break;
229+
}
230+
}
231+
NameSV[S++] = C;
232+
}
233+
NameSV.resize(S);
234+
NameRef = NameSV;
235+
}
215236

216237
assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
217238

llvm/lib/MC/MCSymbol.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
7474
OS << "\\n";
7575
else if (C == '"')
7676
OS << "\\\"";
77+
else if (C == '\\')
78+
OS << "\\\\";
7779
else
7880
OS << C;
7981
}

llvm/test/MC/AsmParser/quoted.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"a b":
1010
call "a b"
1111

12+
# CHECK: "a b\\":
13+
"a b\\":
14+
1215
#--- err.s
1316
"a\":
1417
# ERR: 1:2: error: unterminated string constant

llvm/test/MC/COFF/safeseh.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
// check that we quote the output of .safeseh
44

5-
.safeseh "\01foo"
6-
// CHECK: .safeseh "\01foo"
5+
.safeseh "\\foo"
6+
// CHECK: .safeseh "\\foo"

llvm/test/MC/ELF/symbol-names.s

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
// RUN: llvm-mc -triple i686-pc-linux -filetype=obj %s -o - | llvm-readobj --symbols - | FileCheck %s
1+
// RUN: llvm-mc -triple=x86_64 -filetype=obj %s | llvm-objdump -tdr - | FileCheck %s
22

33
// MC allows ?'s in symbol names as an extension.
44

5+
// CHECK-LABEL:SYMBOL TABLE:
6+
// CHECK-NEXT: 0000000000000001 l F .text 0000000000000000 a"b\{{$}}
7+
// CHECK-NEXT: 0000000000000006 l .text 0000000000000000 a\{{$}}
8+
// CHECK-NEXT: 0000000000000000 g F .text 0000000000000000 foo?bar
9+
// CHECK-NEXT: 0000000000000000 *UND* 0000000000000000 a"b\q{{$}}
10+
// CHECK-EMPTY:
11+
512
.text
613
.globl foo?bar
714
.type foo?bar, @function
815
foo?bar:
916
ret
1017

11-
// CHECK: Symbol
12-
// CHECK: Name: foo?bar
18+
// CHECK-LABEL:<a"b\>:
19+
// CHECK-NEXT: callq {{.*}} <a"b\>
20+
// CHECK-LABEL:<a\>:
21+
// CHECK-NEXT: callq {{.*}}
22+
// CHECK-NEXT: R_X86_64_PLT32 a"b\q-0x4
23+
.type "a\"b\\", @function
24+
"a\"b\\":
25+
call "a\"b\\"
26+
"a\\":
27+
/// GAS emits a warning for \q
28+
call "a\"b\q"

0 commit comments

Comments
 (0)