Skip to content

Commit

Permalink
being stricter with the fixups when assembling
Browse files Browse the repository at this point in the history
  • Loading branch information
snf committed Mar 7, 2016
1 parent 50fb886 commit f6051e2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
16 changes: 11 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ fn main() {
let out_obj = format!("{}/assemble.o", out_dir);
let out_lib = format!("{}/libassemble.a", out_dir);

Command::new("g++").args(&["src/c/assemble.cc", "-c", "-fPIC", "-o"])
if !Command::new("g++").args(&["src/c/assemble.cc", "-g", "-c", "-fPIC", "-o"])
.arg(&out_obj)
.args(&llvm_cxxflags)
.status().unwrap();
.status().unwrap().success()
{
panic!("g++ failed to build");
}

Command::new("ar").args(&["crus", &out_lib, &out_obj])
.current_dir(&Path::new(&out_dir))
.status().unwrap();
if !Command::new("ar").args(&["crus", &out_lib, &out_obj])
.current_dir(&Path::new(&out_dir))
.status().unwrap().success()
{
panic!("ar failed to build");
}

print!("cargo:rustc-flags=");
for path in llvm_ldflags {
Expand Down
56 changes: 34 additions & 22 deletions src/c/assemble.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ class MCBinaryStreamer: public MCStreamer {
std::unique_ptr<MCCodeEmitter> Emitter;
std::unique_ptr<MCAsmBackend> AsmBackend;
std::vector<byte> &OutMb;
bool *Failed;
size_t pos = 0;

MCBinaryStreamer(MCContext &Context, MCAsmBackend *TAB, raw_pwrite_stream &OS,
MCCodeEmitter *emitter, std::vector<byte> &out_mb)
MCCodeEmitter *emitter, std::vector<byte> &out_mb, bool *failed)
: MCStreamer(Context), OS(OS), MAI(Context.getAsmInfo()),
Emitter(emitter), AsmBackend(TAB), OutMb(out_mb) {
Emitter(emitter), AsmBackend(TAB), OutMb(out_mb), Failed(failed) {
OS << "starting";
OS << '\n';
}

bool EmitSymbolAttribute(MCSymbol *Symbol,
MCSymbolAttr Attribute) { return false; }

Expand All @@ -68,15 +69,20 @@ class MCBinaryStreamer: public MCStreamer {
for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
FixupMap[i] = 0;

for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
MCFixup &F = Fixups[i];
const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
for (unsigned j = 0; j != Info.TargetSize; ++j) {
unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
FixupMap[Index] = 1 + i;
}
if (Fixups.size() != 0 || Code.size() == 0) {
*Failed = true;
return;
}
// XXX_ [0] disabling fixups because AsmBackend is null
// for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
// MCFixup &F = Fixups[i];
// const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
// for (unsigned j = 0; j != Info.TargetSize; ++j) {
// unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
// assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
// FixupMap[Index] = 1 + i;
// }
// }

// FIXME: Note the fixup comments for Thumb2 are completely bogus since the
// high order halfword of a 32-bit Thumb2 instruction is emitted first.
Expand All @@ -87,7 +93,7 @@ class MCBinaryStreamer: public MCStreamer {
// Copy to memory buffer
//OutMb[pos] = Code[i];
OutMb.push_back(Code[i]);

pos++;

if (i)
Expand Down Expand Up @@ -136,12 +142,13 @@ class MCBinaryStreamer: public MCStreamer {
}
OS << "]\n";

for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
MCFixup &F = Fixups[i];
const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
<< ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
}
// XXX_ disabling fixups because [0]
// for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
// MCFixup &F = Fixups[i];
// const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
// OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
// << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
// }
}
};

Expand Down Expand Up @@ -253,7 +260,7 @@ static int assemble_llvm(StringRef &arch, StringRef &input_str, std::vector<byte
BufferPtr = MemoryBuffer::getMemBuffer(input_str);
}


SourceMgr SrcMgr;

// Tell SrcMgr about this buffer, which is what the parser will pick up.
Expand Down Expand Up @@ -304,13 +311,18 @@ static int assemble_llvm(StringRef &arch, StringRef &input_str, std::vector<byte

out_bytes.reserve(0x100);

Str.reset(new MCBinaryStreamer(Ctx, MAB, *OS, CE, out_bytes));
bool failed = false;
Str.reset(new MCBinaryStreamer(Ctx, MAB, *OS, CE, out_bytes, &failed));

int Res = 1;
// XXX_ remember the last arg is only for X86
Res = AssembleInput(TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI,
*MCII, MCOptions, 1);

if (failed == true) {
return -1;
}

return Res;
}

Expand Down Expand Up @@ -367,8 +379,8 @@ int assemble(enum Arch arch, const char *instructions, byte **out, size_t *out_l
int main(int argc, char **argv) {
StringRef arch(argv[1]);
StringRef input("-");
std::vector<byte> out_bytes;
std::vector<byte> out_bytes;
assemble_llvm(arch, input, out_bytes);
}
*/

0 comments on commit f6051e2

Please sign in to comment.