Skip to content

Commit

Permalink
[DWARFv5] Fuss with asm syntax for conveying MD5 checksum.
Browse files Browse the repository at this point in the history
Previously the MD5 option of the .file directive provided the checksum
as a quoted hex string; now it's a normal hex number with 0x prefix,
same as the .octa directive accepts.

Differential Revision: https://reviews.llvm.org/D45459

llvm-svn: 329820
  • Loading branch information
pogo59 committed Apr 11, 2018
1 parent 366857a commit 0195469
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 42 deletions.
6 changes: 2 additions & 4 deletions llvm/lib/MC/MCAsmStreamer.cpp
Expand Up @@ -1106,10 +1106,8 @@ static void printDwarfFileDirective(unsigned FileNo, StringRef Directory,
OS << ' ';
}
PrintQuotedString(Filename, OS);
if (Checksum) {
OS << " md5 ";
PrintQuotedString(Checksum->digest(), OS);
}
if (Checksum)
OS << " md5 0x" << Checksum->digest();
if (Source) {
OS << " source ";
PrintQuotedString(*Source, OS);
Expand Down
54 changes: 30 additions & 24 deletions llvm/lib/MC/MCParser/AsmParser.cpp
Expand Up @@ -2969,28 +2969,35 @@ bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) {
return false;
}

static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) {
if (Asm.getTok().isNot(AsmToken::Integer) &&
Asm.getTok().isNot(AsmToken::BigNum))
return Asm.TokError("unknown token in expression");
SMLoc ExprLoc = Asm.getTok().getLoc();
APInt IntValue = Asm.getTok().getAPIntVal();
Asm.Lex();
if (!IntValue.isIntN(128))
return Asm.Error(ExprLoc, "out of range literal value");
if (!IntValue.isIntN(64)) {
hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
lo = IntValue.getLoBits(64).getZExtValue();
} else {
hi = 0;
lo = IntValue.getZExtValue();
}
return false;
}

/// ParseDirectiveOctaValue
/// ::= .octa [ hexconstant (, hexconstant)* ]

bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) {
auto parseOp = [&]() -> bool {
if (checkForValidSection())
return true;
if (getTok().isNot(AsmToken::Integer) && getTok().isNot(AsmToken::BigNum))
return TokError("unknown token in expression");
SMLoc ExprLoc = getTok().getLoc();
APInt IntValue = getTok().getAPIntVal();
uint64_t hi, lo;
Lex();
if (!IntValue.isIntN(128))
return Error(ExprLoc, "out of range literal value");
if (!IntValue.isIntN(64)) {
hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue();
lo = IntValue.getLoBits(64).getZExtValue();
} else {
hi = 0;
lo = IntValue.getZExtValue();
}
if (parseHexOcta(*this, hi, lo))
return true;
if (MAI.isLittleEndian()) {
getStreamer().EmitIntValue(lo, 8);
getStreamer().EmitIntValue(hi, 8);
Expand Down Expand Up @@ -3283,7 +3290,8 @@ bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
Filename = Path;
}

std::string Checksum;
uint64_t MD5Hi, MD5Lo;
bool HasMD5 = false;

Optional<StringRef> Source;
bool HasSource = false;
Expand All @@ -3296,12 +3304,10 @@ bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
parseIdentifier(Keyword))
return true;
if (Keyword == "md5") {
HasMD5 = true;
if (check(FileNumber == -1,
"MD5 checksum specified, but no file number") ||
check(getTok().isNot(AsmToken::String),
"unexpected token in '.file' directive") ||
parseEscapedString(Checksum) ||
check(Checksum.size() != 32, "invalid MD5 checksum specified"))
parseHexOcta(*this, MD5Hi, MD5Lo))
return true;
} else if (Keyword == "source") {
HasSource = true;
Expand All @@ -3324,12 +3330,12 @@ bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
getStreamer().EmitFileDirective(Filename);
else {
MD5::MD5Result *CKMem = nullptr;
if (!Checksum.empty()) {
Checksum = fromHex(Checksum);
if (check(Checksum.size() != 16, "invalid MD5 checksum specified"))
return true;
if (HasMD5) {
CKMem = (MD5::MD5Result *)Ctx.allocate(sizeof(MD5::MD5Result), 1);
memcpy(&CKMem->Bytes, Checksum.data(), 16);
for (unsigned i = 0; i != 8; ++i) {
CKMem->Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8));
CKMem->Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8));
}
}
if (HasSource) {
char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size()));
Expand Down
6 changes: 3 additions & 3 deletions llvm/test/CodeGen/Generic/dwarf-md5.ll
Expand Up @@ -13,13 +13,13 @@
; RUN: llvm-dwarfdump -debug-line %t5.o | FileCheck %s --check-prefixes=OBJ,OBJ-5

; ASM-4-NOT: .file 0
; ASM-5: .file 0 "/scratch{{.*[/\\]}}t.c" md5 "00000000000000000000000000000000"
; ASM-5: .file 0 "/scratch{{.*[/\\]}}t.c" md5 0x00000000000000000000000000000000
; ASM: .file 1 "/scratch{{.*[/\\]}}t1.h"
; ASM-4-NOT: md5
; ASM-5-SAME: md5 "11111111111111111111111111111111"
; ASM-5-SAME: md5 0x11111111111111111111111111111111
; ASM: .file 2 "/scratch{{.*[/\\]}}t2.h"
; ASM-4-NOT: md5
; ASM-5-SAME: md5 "22222222222222222222222222222222"
; ASM-5-SAME: md5 0x22222222222222222222222222222222

; OBJ-5: file_names[ 0]:
; OBJ-5-NEXT: name: "t.c"
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/MC/ELF/debug-file-options.s
Expand Up @@ -2,8 +2,8 @@

// Test combinations of options to the .file directive.

.file 1 "dir1/foo" md5 "ee87e05688663173cd6043a3a15bba6e" source "void foo() {}"
.file 2 "dir2/bar" source "void bar() {}" md5 "816225a0c90ca8948b70eb58be4d522f"
.file 1 "dir1/foo" md5 0xee87e05688663173cd6043a3a15bba6e source "void foo() {}"
.file 2 "dir2/bar" source "void bar() {}" md5 0x816225a0c90ca8948b70eb58be4d522f
.loc 1 1 0
nop
.loc 2 1 0
Expand Down
14 changes: 7 additions & 7 deletions llvm/test/MC/ELF/debug-md5-err.s
Expand Up @@ -7,20 +7,20 @@

# Missing md5 keyword.
# CHECK: [[@LINE+1]]:{{[0-9]+}}: error: unexpected token in '.file' directive
.file 2 "dir1" "foo" "00112233445566778899aabbccddeeff"
.file 2 "dir1" "foo" 0x00112233445566778899aabbccddeeff

# Bad length.
# CHECK: [[@LINE+1]]:{{[0-9]+}}: error: invalid MD5 checksum specified
# Bad syntax.
# CHECK: [[@LINE+1]]:{{[0-9]+}}: error: unknown token in expression
.file 3 "dir2" "bar" md5 "ff"

# Not a string.
# CHECK: [[@LINE+1]]:{{[0-9]+}}: error: unexpected token in '.file' directive
# No hex prefix.
# CHECK: [[@LINE+1]]:{{[0-9]+}}: error: unknown token in expression
.file 4 "dir3" "foo" md5 ffeeddccbbaa99887766554433221100

# Non-DWARF .file syntax with checksum.
# CHECK: [[@LINE+1]]:{{[0-9]+}}: error: MD5 checksum specified, but no file number
.file "baz" md5 "ffeeddccbbaa998877665544332211gg"
.file "baz" md5 0xffeeddccbbaa99887766554433221100

# Inconsistent use of MD5 option. Note: .file 1 did not supply one.
# CHECK: [[@LINE+1]]:{{[0-9]+}}: error: inconsistent use of MD5 checksums
.file 5 "bax" md5 "ffeeddccbbaa99887766554433221100"
.file 5 "bax" md5 0xffeeddccbbaa99887766554433221100
4 changes: 2 additions & 2 deletions llvm/test/MC/ELF/debug-md5.s
@@ -1,7 +1,7 @@
// RUN: llvm-mc -triple x86_64-unknown-unknown -dwarf-version 5 -fdebug-compilation-dir=/tmp -filetype=obj %s -o - | llvm-dwarfdump --debug-line --debug-line-str -v - | FileCheck %s

.file 1 "dir1/foo" md5 "00112233445566778899aabbccddeeff"
.file 2 "dir2" "bar" md5 "ffeeddccbbaa99887766554433221100"
.file 1 "dir1/foo" md5 0x00112233445566778899aabbccddeeff
.file 2 "dir2" "bar" md5 0xffeeddccbbaa99887766554433221100
.loc 1 1 0
nop
.loc 2 1 0
Expand Down

0 comments on commit 0195469

Please sign in to comment.