Reference reference
To emit a new section, e.g. .newsection
, to an object (ELF) file:
add an option to clang to enable options.
TargetOptions::EmitNewSection
TargetMachine
instance: TM.Options.EmitNewSection
AsmPrinter
, etc.MachineFunction
instance: MF.getTarget().Options.EmitNewSection
update AsmPrinter
class in LLVM CodeGen to emit the new section. e.g. AsmPrinter::emitNewSection()
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
add target dependent interface in AsmPrinter for each target to implement the actual emission for the section. e.g. virtual void EmitNewSectionEntry();
llvm/lib/Target/Mips/MipsAsmPrinter.cpp
add a new MCSection
member, say MCSection *NewSection;
, to MCObjectFileInfo
class.
MCSection *getNewSection(){...}
Update initialization code of MCObjectFileInfo
to create the new section instance.
MCObjectFileInfo::initELFMCObjectFileInfo()
:
void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
...
NewSection = Ctx->getELFSection(".newsection",
ELF::SHT_PROGBITS,
ELF::SHF_ALLOC);
...
}
Register the section to the MCAssembler
in MipsTargetELFStreamer::finish()
:
void MipsTargetELFStreamer::finish() {
MCAssembler &MCA = getStreamer().getAssembler();
const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo();
...
// add new section .newsection.
MCSection &NewSection = *OFI.getNewSection();
MCA.registerSection(NewSection);
...
}
Update DumpStyle
class with new interface to print the new section; and implement it in the children class, such as GNUStyle
, LLVMStyle
, etc. // currently not used.
Switch current section to the new section and emit the section entries one by one:
// llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
AsmPrinter::emitNewSection(const MachineFunction &MF){
...
MCSection *NewSection = getObjFileLowering().getNewSection();
OutStreamer->PushSection();
OutStreamer->SwitchSection(NewSection);
for (auto &MBB : MF) {
for (auto &MI : MBB) {
EmitNewSectionEntry(&MI);
}
}
OutStreamer->PopSection();
}
If you could revise
the fundmental principles of
computer system design
to improve security...
... what would you change?