Skip to content

Commit cb7c040

Browse files
committed
Address review comments
1 parent 15faa57 commit cb7c040

File tree

8 files changed

+35
-19
lines changed

8 files changed

+35
-19
lines changed

llvm/include/llvm/MC/MCAssembler.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,12 @@ class MCAssembler {
154154
bool SubsectionsViaSymbols : 1;
155155
bool IncrementalLinkerCompatible : 1;
156156

157-
/// ELF specific e_header and abi version flags
158-
// It would be good if there were an MCELFAssembler class to hold these. ELF
159-
// header flags are used both by the integrated and standalone assemblers.
157+
/// ELF specific e_header flags
158+
// It would be good if there were an MCELFAssembler class to hold this.
159+
// ELF header flags are used both by the integrated and standalone assemblers.
160160
// Access to the flags is necessary in cases where assembler directives affect
161161
// which flags to be set.
162162
unsigned ELFHeaderEFlags;
163-
unsigned char ELFHeaderABIVersion = 0;
164163

165164
/// Used to communicate Linker Optimization Hint information between
166165
/// the Streamer and the .o writer
@@ -280,10 +279,6 @@ class MCAssembler {
280279
unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
281280
void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
282281

283-
/// ELF e_ident[EI_ABIVERSION] value
284-
unsigned char getELFHeaderABIVersion() const { return ELFHeaderABIVersion; }
285-
void setELFHeaderABIVersion(unsigned char V) { ELFHeaderABIVersion = V; }
286-
287282
/// MachO deployment target version information.
288283
const VersionInfoType &getVersionInfo() const { return VersionInfo; }
289284
void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,

llvm/include/llvm/MC/MCELFObjectWriter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ struct ELFRelocationEntry {
5252

5353
class MCELFObjectTargetWriter : public MCObjectTargetWriter {
5454
const uint8_t OSABI;
55+
const uint8_t ABIVersion;
5556
const uint16_t EMachine;
5657
const unsigned HasRelocationAddend : 1;
5758
const unsigned Is64Bit : 1;
5859

5960
protected:
6061
MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
61-
bool HasRelocationAddend_);
62+
bool HasRelocationAddend_, uint8_t ABIVersion_ = 0);
6263

6364
public:
6465
virtual ~MCELFObjectTargetWriter() = default;
@@ -96,6 +97,7 @@ class MCELFObjectTargetWriter : public MCObjectTargetWriter {
9697
/// \name Accessors
9798
/// @{
9899
uint8_t getOSABI() const { return OSABI; }
100+
uint8_t getABIVersion() const { return ABIVersion; }
99101
uint16_t getEMachine() const { return EMachine; }
100102
bool hasRelocationAddend() const { return HasRelocationAddend; }
101103
bool is64Bit() const { return Is64Bit; }

llvm/include/llvm/MC/MCObjectWriter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class MCObjectWriter {
9292
/// ELF only. Mark that we have seen GNU ABI usage (e.g. SHF_GNU_RETAIN).
9393
virtual void markGnuAbi() {}
9494

95+
/// ELF only, override the default ABIVersion in the ELF header.
96+
virtual void setOverrideABIVersion(uint8_t ABIVersion) {}
97+
9598
/// Tell the object writer to emit an address-significance table during
9699
/// writeObject(). If this function is not called, all symbols are treated as
97100
/// address-significant.

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ class ELFObjectWriter : public MCObjectWriter {
226226

227227
bool SeenGnuAbi = false;
228228

229+
std::optional<uint8_t> OverrideABIVersion;
230+
229231
bool hasRelocationAddend() const;
230232

231233
bool shouldRelocateWithSymbol(const MCAssembler &Asm, const MCValue &Val,
@@ -238,6 +240,7 @@ class ELFObjectWriter : public MCObjectWriter {
238240

239241
void reset() override {
240242
SeenGnuAbi = false;
243+
OverrideABIVersion.reset();
241244
Relocations.clear();
242245
Renames.clear();
243246
MCObjectWriter::reset();
@@ -264,6 +267,10 @@ class ELFObjectWriter : public MCObjectWriter {
264267
void markGnuAbi() override { SeenGnuAbi = true; }
265268
bool seenGnuAbi() const { return SeenGnuAbi; }
266269

270+
bool seenOverrideABIVersion() const { return OverrideABIVersion.has_value(); }
271+
uint8_t getOverrideABIVersion() const { return OverrideABIVersion.value(); }
272+
void setOverrideABIVersion(uint8_t V) override { OverrideABIVersion = V; }
273+
267274
friend struct ELFWriter;
268275
};
269276

@@ -417,7 +424,9 @@ void ELFWriter::writeHeader(const MCAssembler &Asm) {
417424
? int(ELF::ELFOSABI_GNU)
418425
: OSABI);
419426
// e_ident[EI_ABIVERSION]
420-
W.OS << char(Asm.getELFHeaderABIVersion());
427+
W.OS << char(OWriter.seenOverrideABIVersion()
428+
? OWriter.getOverrideABIVersion()
429+
: OWriter.TargetObjectWriter->getABIVersion());
421430

422431
W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
423432

llvm/lib/MC/MCELFObjectTargetWriter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ using namespace llvm;
1212

1313
MCELFObjectTargetWriter::MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
1414
uint16_t EMachine_,
15-
bool HasRelocationAddend_)
16-
: OSABI(OSABI_), EMachine(EMachine_),
15+
bool HasRelocationAddend_,
16+
uint8_t ABIVersion_)
17+
: OSABI(OSABI_), ABIVersion(ABIVersion_), EMachine(EMachine_),
1718
HasRelocationAddend(HasRelocationAddend_), Is64Bit(Is64Bit_) {}
1819

1920
bool MCELFObjectTargetWriter::needsRelocateWithSymbol(const MCValue &,

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/MC/MCAssembler.h"
2121
#include "llvm/MC/MCContext.h"
2222
#include "llvm/MC/MCELFStreamer.h"
23+
#include "llvm/MC/MCObjectWriter.h"
2324
#include "llvm/MC/MCSectionELF.h"
2425
#include "llvm/MC/MCSubtargetInfo.h"
2526
#include "llvm/Support/AMDGPUMetadata.h"
@@ -485,7 +486,7 @@ MCELFStreamer &AMDGPUTargetELFStreamer::getStreamer() {
485486
void AMDGPUTargetELFStreamer::finish() {
486487
MCAssembler &MCA = getStreamer().getAssembler();
487488
MCA.setELFHeaderEFlags(getEFlags());
488-
MCA.setELFHeaderABIVersion(
489+
MCA.getWriter().setOverrideABIVersion(
489490
getELFABIVersion(STI.getTargetTriple(), CodeObjectVersion));
490491

491492
std::string Blob;

llvm/test/MC/AMDGPU/elf-header-cov.s

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@
44
// RUN: sed 's/COV/5/g' %s | llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx802 -filetype=obj | \
55
// RUN: llvm-readobj --file-headers - | FileCheck %s --check-prefixes=HS5
66

7+
// RUN: sed 's/COV/4/g' %s | not llvm-mc -triple amdgcn-amd-amdpal -mcpu=gfx802 2>&1 | \
8+
// RUN: FileCheck %s --check-prefix=ERR
9+
10+
// RUN: sed 's/COV/4/g' %s | not llvm-mc -triple amdgcn-amd-mesa3d -mcpu=gfx802 2>&1 | \
11+
// RUN: FileCheck %s --check-prefix=ERR
12+
13+
// RUN: sed 's/COV/4/g' %s | not llvm-mc -triple amdgcn-amd- -mcpu=gfx802 2>&1 | \
14+
// RUN: FileCheck %s --check-prefix=ERR
15+
716
.amdhsa_code_object_version COV
817

18+
// ERR: error: unknown directive
19+
920
// HS4: OS/ABI: AMDGPU_HSA (0x40)
1021
// HS4-NEXT: ABIVersion: 2
1122

llvm/test/MC/AMDGPU/hsa-cov-invalid.s

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)