[MC][RISCV] Add RISCV MCObjectFileInfo

This makes sure, that the text section will have a 2-byte alignment, if
the +c extension is enabled.

Reviewed By: MaskRay, luismarques

Differential Revision: https://reviews.llvm.org/D102052
This commit is contained in:
Philipp Krones 2021-08-27 17:55:52 +01:00 committed by Simon Cook
parent 0eeab8b282
commit 54e8cae565
8 changed files with 71 additions and 4 deletions

View File

@ -231,6 +231,7 @@ protected:
public:
void initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
bool LargeCodeModel = false);
virtual ~MCObjectFileInfo();
MCContext &getContext() const { return *Ctx; }
bool getSupportsWeakOmittedEHFrame() const {
@ -253,6 +254,7 @@ public:
return CompactUnwindDwarfEHFrameOnly;
}
virtual unsigned getTextSectionAlignment() const { return 4; }
MCSection *getTextSection() const { return TextSection; }
MCSection *getDataSection() const { return DataSection; }
MCSection *getBSSSection() const { return BSSSection; }

View File

@ -91,7 +91,7 @@ void MCELFStreamer::mergeFragment(MCDataFragment *DF,
void MCELFStreamer::InitSections(bool NoExecStack) {
MCContext &Ctx = getContext();
SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
emitCodeAlignment(4);
emitCodeAlignment(Ctx.getObjectFileInfo()->getTextSectionAlignment());
if (NoExecStack)
SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));

View File

@ -981,6 +981,8 @@ void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
/* MultiSymbolsAllowed */ true, ".dwmac", XCOFF::SSUBTYP_DWMAC);
}
MCObjectFileInfo::~MCObjectFileInfo() {}
void MCObjectFileInfo::initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
bool LargeCodeModel) {
PositionIndependent = PIC;

View File

@ -6,6 +6,7 @@ add_llvm_component_library(LLVMRISCVDesc
RISCVMCAsmInfo.cpp
RISCVMCCodeEmitter.cpp
RISCVMCExpr.cpp
RISCVMCObjectFileInfo.cpp
RISCVMCTargetDesc.cpp
RISCVMatInt.cpp
RISCVTargetStreamer.cpp

View File

@ -0,0 +1,22 @@
//===-- RISCVMCObjectFileInfo.cpp - RISCV object file properties ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains the declarations of the RISCVMCObjectFileInfo properties.
//
//===----------------------------------------------------------------------===//
#include "RISCVMCObjectFileInfo.h"
#include "RISCVMCTargetDesc.h"
#include "llvm/MC/MCContext.h"
using namespace llvm;
unsigned RISCVMCObjectFileInfo::getTextSectionAlignment() const {
const MCSubtargetInfo *STI = getContext().getSubtargetInfo();
return STI->hasFeature(RISCV::FeatureStdExtC) ? 2 : 4;
}

View File

@ -0,0 +1,27 @@
//===-- RISCVMCObjectFileInfo.h - RISCV object file Info -------*- C++ -*--===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains the declaration of the RISCVMCObjectFileInfo class.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCOBJECTFILEINFO_H
#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCOBJECTFILEINFO_H
#include "llvm/MC/MCObjectFileInfo.h"
namespace llvm {
class RISCVMCObjectFileInfo : public MCObjectFileInfo {
public:
unsigned getTextSectionAlignment() const override;
};
} // namespace llvm
#endif

View File

@ -15,6 +15,7 @@
#include "RISCVELFStreamer.h"
#include "RISCVInstPrinter.h"
#include "RISCVMCAsmInfo.h"
#include "RISCVMCObjectFileInfo.h"
#include "RISCVTargetStreamer.h"
#include "TargetInfo/RISCVTargetInfo.h"
#include "llvm/ADT/STLExtras.h"
@ -23,6 +24,7 @@
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCInstrAnalysis.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCStreamer.h"
@ -65,6 +67,14 @@ static MCAsmInfo *createRISCVMCAsmInfo(const MCRegisterInfo &MRI,
return MAI;
}
static MCObjectFileInfo *
createRISCVMCObjectFileInfo(MCContext &Ctx, bool PIC,
bool LargeCodeModel = false) {
MCObjectFileInfo *MOFI = new RISCVMCObjectFileInfo();
MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel);
return MOFI;
}
static MCSubtargetInfo *createRISCVMCSubtargetInfo(const Triple &TT,
StringRef CPU, StringRef FS) {
if (CPU.empty())
@ -155,6 +165,7 @@ MCStreamer *createRISCVELFStreamer(const Triple &T, MCContext &Context,
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTargetMC() {
for (Target *T : {&getTheRISCV32Target(), &getTheRISCV64Target()}) {
TargetRegistry::RegisterMCAsmInfo(*T, createRISCVMCAsmInfo);
TargetRegistry::RegisterMCObjectFileInfo(*T, createRISCVMCObjectFileInfo);
TargetRegistry::RegisterMCInstrInfo(*T, createRISCVMCInstrInfo);
TargetRegistry::RegisterMCRegInfo(*T, createRISCVMCRegisterInfo);
TargetRegistry::RegisterMCAsmBackend(*T, createRISCVAsmBackend);

View File

@ -33,13 +33,15 @@
# Linker could satisfy alignment by removing NOPs after linker relaxation.
# The first R_RISCV_ALIGN come from
# MCELFStreamer::InitSections() emitCodeAlignment(4).
# MCELFStreamer::InitSections() emitCodeAlignment(getTextSectionAligntment()).
# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x2
# C-EXT-RELAX-INST: c.nop
test:
.p2align 2
# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x2
# C-EXT-RELAX-INST: c.nop
# If the +c extension is enabled, the text section will be 2-byte aligned, so
# one c.nop instruction is sufficient.
# C-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN - 0x2
# C-EXT-RELAX-INST-NOT: c.nop
bne zero, a0, .LBB0_2
mv a0, zero
.p2align 3