blob: 3ef7e37206daa8ab1fa07d806e411642d522e1aa [file] [log] [blame]
//===- lib/MC/MCNaCl.cpp - NaCl-specific MC implementation ----------------===//
//
// 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 was written by the Native Client authors.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCNaCl.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCNaClExpander.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/TargetParser/Triple.h"
static const char NoteNamespace[] = "NaCl";
namespace llvm {
cl::opt<bool> FlagEnableAutoSandboxing("nacl-enable-autosandboxing",
cl::desc("Don't use the autosandboxing"
" assembler for the NaCl SFI."),
cl::init(true));
void initializeNaClMCStreamer(MCStreamer &Streamer, MCContext &Ctx,
const Triple &TheTriple) {
assert(TheTriple.isOSNaCl());
const char *NoteName;
const char *NoteArch;
Align BundleAlign;
switch (TheTriple.getArch()) {
case Triple::arm:
NoteName = ".note.NaCl.ABI.arm";
NoteArch = "arm";
BundleAlign = Align(16);
break;
case Triple::mipsel:
NoteName = ".note.NaCl.ABI.mipsel";
NoteArch = "mipsel";
BundleAlign = Align(16);
break;
case Triple::x86:
NoteName = ".note.NaCl.ABI.x86-32";
NoteArch = "x86-32";
BundleAlign = Align(32);
break;
case Triple::x86_64:
NoteName = ".note.NaCl.ABI.x86-64";
NoteArch = "x86-64";
BundleAlign = Align(32);
break;
default:
report_fatal_error("Unsupported architecture for NaCl");
}
std::string Error; //empty
const Target *TheTarget =
TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
// Create the Target specific MCNaClExpander
assert(TheTarget != nullptr);
if (FlagEnableAutoSandboxing) {
TheTarget->createMCNaClExpander(
Streamer,
std::unique_ptr<MCRegisterInfo>(
TheTarget->createMCRegInfo(TheTriple.getTriple())),
std::unique_ptr<MCInstrInfo>(TheTarget->createMCInstrInfo()));
}
// Set bundle-alignment as required by the NaCl ABI for the target.
Streamer.emitBundleAlignMode(BundleAlign);
// For gas, override the size of DWARF address values generated by .loc
// directives.
if (TheTriple.getArch() == Triple::x86_64 &&
Streamer.hasRawTextSupport()) {
Streamer.emitRawText("\t.dwarf_addr_size 4\n");
}
// Emit an ELF Note section in its own COMDAT group which identifies NaCl
// object files to the gold linker, so it can use the NaCl layout.
MCSectionELF* Note = Ctx.getELFSection(NoteName, ELF::SHT_NOTE,
ELF::SHF_ALLOC | ELF::SHF_GROUP,
0, NoteName, /*IsComdat=*/true);
Streamer.pushSection();
Streamer.switchSection(Note);
Streamer.emitIntValue(strlen(NoteNamespace) + 1, 4);
Streamer.emitIntValue(strlen(NoteArch) + 1, 4);
Streamer.emitIntValue(ELF::NT_VERSION, 4);
Streamer.emitBytes(NoteNamespace);
Streamer.emitIntValue(0, 1); // NUL terminator
Streamer.emitValueToAlignment(Align(4));
Streamer.emitBytes(NoteArch);
Streamer.emitIntValue(0, 1); // NUL terminator
Streamer.emitValueToAlignment(Align(4));
Streamer.popSection();
}
} // namespace llvm