2020-01-21 22:32:00 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
const ArenaAllocator = std.heap.ArenaAllocator;
|
|
|
|
const ArrayList = std.ArrayList;
|
|
|
|
const Builder = std.build.Builder;
|
|
|
|
const File = std.fs.File;
|
|
|
|
const InstallDir = std.build.InstallDir;
|
|
|
|
const LibExeObjStep = std.build.LibExeObjStep;
|
|
|
|
const Step = std.build.Step;
|
|
|
|
const elf = std.elf;
|
|
|
|
const fs = std.fs;
|
|
|
|
const io = std.io;
|
|
|
|
const sort = std.sort;
|
|
|
|
const warn = std.debug.warn;
|
|
|
|
|
|
|
|
const BinaryElfSection = struct {
|
|
|
|
elfOffset: u64,
|
|
|
|
binaryOffset: u64,
|
|
|
|
fileSize: usize,
|
|
|
|
segment: ?*BinaryElfSegment,
|
|
|
|
};
|
|
|
|
|
|
|
|
const BinaryElfSegment = struct {
|
|
|
|
physicalAddress: u64,
|
|
|
|
virtualAddress: u64,
|
|
|
|
elfOffset: u64,
|
|
|
|
binaryOffset: u64,
|
|
|
|
fileSize: usize,
|
|
|
|
firstSection: ?*BinaryElfSection,
|
|
|
|
};
|
|
|
|
|
|
|
|
const BinaryElfOutput = struct {
|
|
|
|
segments: ArrayList(*BinaryElfSegment),
|
|
|
|
sections: ArrayList(*BinaryElfSection),
|
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
|
|
self.sections.deinit();
|
|
|
|
self.segments.deinit();
|
|
|
|
}
|
|
|
|
|
2020-03-11 02:01:58 +00:00
|
|
|
pub fn parse(allocator: *Allocator, elf_file: File) !Self {
|
|
|
|
var self: Self = .{
|
|
|
|
.segments = ArrayList(*BinaryElfSegment).init(allocator),
|
|
|
|
.sections = ArrayList(*BinaryElfSection).init(allocator),
|
|
|
|
};
|
2020-08-01 01:37:02 +01:00
|
|
|
const elf_hdr = try std.elf.readHeader(elf_file);
|
2020-01-21 22:32:00 +00:00
|
|
|
|
2020-08-01 01:37:02 +01:00
|
|
|
var section_headers = elf_hdr.section_header_iterator(elf_file);
|
|
|
|
while (try section_headers.next()) |section| {
|
2020-01-21 22:32:00 +00:00
|
|
|
if (sectionValidForOutput(section)) {
|
|
|
|
const newSection = try allocator.create(BinaryElfSection);
|
|
|
|
|
|
|
|
newSection.binaryOffset = 0;
|
|
|
|
newSection.elfOffset = section.sh_offset;
|
|
|
|
newSection.fileSize = @intCast(usize, section.sh_size);
|
|
|
|
newSection.segment = null;
|
|
|
|
|
|
|
|
try self.sections.append(newSection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 01:37:02 +01:00
|
|
|
var program_headers = elf_hdr.program_header_iterator(elf_file);
|
|
|
|
while (try program_headers.next()) |phdr| {
|
2020-03-11 02:01:58 +00:00
|
|
|
if (phdr.p_type == elf.PT_LOAD) {
|
2020-01-21 22:32:00 +00:00
|
|
|
const newSegment = try allocator.create(BinaryElfSegment);
|
|
|
|
|
2020-03-11 02:01:58 +00:00
|
|
|
newSegment.physicalAddress = if (phdr.p_paddr != 0) phdr.p_paddr else phdr.p_vaddr;
|
|
|
|
newSegment.virtualAddress = phdr.p_vaddr;
|
|
|
|
newSegment.fileSize = @intCast(usize, phdr.p_filesz);
|
|
|
|
newSegment.elfOffset = phdr.p_offset;
|
2020-01-21 22:32:00 +00:00
|
|
|
newSegment.binaryOffset = 0;
|
|
|
|
newSegment.firstSection = null;
|
|
|
|
|
2020-03-30 19:23:22 +01:00
|
|
|
for (self.sections.span()) |section| {
|
2020-03-11 02:01:58 +00:00
|
|
|
if (sectionWithinSegment(section, phdr)) {
|
2020-01-21 22:32:00 +00:00
|
|
|
if (section.segment) |sectionSegment| {
|
|
|
|
if (sectionSegment.elfOffset > newSegment.elfOffset) {
|
|
|
|
section.segment = newSegment;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
section.segment = newSegment;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newSegment.firstSection == null) {
|
|
|
|
newSegment.firstSection = section;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try self.segments.append(newSegment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:32:02 +01:00
|
|
|
sort.sort(*BinaryElfSegment, self.segments.span(), {}, segmentSortCompare);
|
2020-01-21 22:32:00 +00:00
|
|
|
|
2020-04-01 23:00:42 +01:00
|
|
|
if (self.segments.items.len > 0) {
|
2020-04-14 03:25:08 +01:00
|
|
|
const firstSegment = self.segments.items[0];
|
2020-01-21 22:32:00 +00:00
|
|
|
if (firstSegment.firstSection) |firstSection| {
|
|
|
|
const diff = firstSection.elfOffset - firstSegment.elfOffset;
|
|
|
|
|
|
|
|
firstSegment.elfOffset += diff;
|
|
|
|
firstSegment.fileSize += diff;
|
|
|
|
firstSegment.physicalAddress += diff;
|
|
|
|
|
|
|
|
const basePhysicalAddress = firstSegment.physicalAddress;
|
|
|
|
|
2020-03-30 19:23:22 +01:00
|
|
|
for (self.segments.span()) |segment| {
|
2020-01-21 22:32:00 +00:00
|
|
|
segment.binaryOffset = segment.physicalAddress - basePhysicalAddress;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:23:22 +01:00
|
|
|
for (self.sections.span()) |section| {
|
2020-01-21 22:32:00 +00:00
|
|
|
if (section.segment) |segment| {
|
|
|
|
section.binaryOffset = segment.binaryOffset + (section.elfOffset - segment.elfOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:32:02 +01:00
|
|
|
sort.sort(*BinaryElfSection, self.sections.span(), {}, sectionSortCompare);
|
2020-03-11 02:01:58 +00:00
|
|
|
|
|
|
|
return self;
|
2020-01-21 22:32:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 02:01:58 +00:00
|
|
|
fn sectionWithinSegment(section: *BinaryElfSection, segment: elf.Elf64_Phdr) bool {
|
2020-01-21 22:32:00 +00:00
|
|
|
return segment.p_offset <= section.elfOffset and (segment.p_offset + segment.p_filesz) >= (section.elfOffset + section.fileSize);
|
|
|
|
}
|
|
|
|
|
2020-07-11 12:09:04 +01:00
|
|
|
fn sectionValidForOutput(shdr: anytype) bool {
|
2020-03-11 02:01:58 +00:00
|
|
|
return shdr.sh_size > 0 and shdr.sh_type != elf.SHT_NOBITS and
|
|
|
|
((shdr.sh_flags & elf.SHF_ALLOC) == elf.SHF_ALLOC);
|
2020-01-21 22:32:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 15:32:02 +01:00
|
|
|
fn segmentSortCompare(context: void, left: *BinaryElfSegment, right: *BinaryElfSegment) bool {
|
2020-01-21 22:32:00 +00:00
|
|
|
if (left.physicalAddress < right.physicalAddress) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (left.physicalAddress > right.physicalAddress) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:32:02 +01:00
|
|
|
fn sectionSortCompare(context: void, left: *BinaryElfSection, right: *BinaryElfSection) bool {
|
2020-01-21 22:32:00 +00:00
|
|
|
return left.binaryOffset < right.binaryOffset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-11 02:01:58 +00:00
|
|
|
fn writeBinaryElfSection(elf_file: File, out_file: File, section: *BinaryElfSection) !void {
|
|
|
|
try out_file.seekTo(section.binaryOffset);
|
2020-01-21 22:32:00 +00:00
|
|
|
|
2020-03-11 02:01:58 +00:00
|
|
|
try out_file.writeFileAll(elf_file, .{
|
|
|
|
.in_offset = section.elfOffset,
|
|
|
|
.in_len = section.fileSize,
|
|
|
|
});
|
2020-01-21 22:32:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 02:01:58 +00:00
|
|
|
fn emitRaw(allocator: *Allocator, elf_path: []const u8, raw_path: []const u8) !void {
|
|
|
|
var elf_file = try fs.cwd().openFile(elf_path, .{});
|
|
|
|
defer elf_file.close();
|
2020-01-21 22:32:00 +00:00
|
|
|
|
2020-03-11 02:01:58 +00:00
|
|
|
var out_file = try fs.cwd().createFile(raw_path, .{});
|
|
|
|
defer out_file.close();
|
2020-01-21 22:32:00 +00:00
|
|
|
|
2020-03-11 19:40:34 +00:00
|
|
|
var binary_elf_output = try BinaryElfOutput.parse(allocator, elf_file);
|
2020-03-11 02:01:58 +00:00
|
|
|
defer binary_elf_output.deinit();
|
2020-01-21 22:32:00 +00:00
|
|
|
|
2020-03-30 19:23:22 +01:00
|
|
|
for (binary_elf_output.sections.span()) |section| {
|
2020-03-11 02:01:58 +00:00
|
|
|
try writeBinaryElfSection(elf_file, out_file, section);
|
2020-01-21 22:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const InstallRawStep = struct {
|
|
|
|
step: Step,
|
|
|
|
builder: *Builder,
|
|
|
|
artifact: *LibExeObjStep,
|
|
|
|
dest_dir: InstallDir,
|
2020-03-04 05:00:42 +00:00
|
|
|
dest_filename: []const u8,
|
2020-01-21 22:32:00 +00:00
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
2020-03-04 05:00:42 +00:00
|
|
|
pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *Self {
|
2020-01-21 22:32:00 +00:00
|
|
|
const self = builder.allocator.create(Self) catch unreachable;
|
|
|
|
self.* = Self{
|
2020-05-25 09:20:31 +01:00
|
|
|
.step = Step.init(.InstallRaw, builder.fmt("install raw binary {}", .{artifact.step.name}), builder.allocator, make),
|
2020-01-21 22:32:00 +00:00
|
|
|
.builder = builder,
|
|
|
|
.artifact = artifact,
|
|
|
|
.dest_dir = switch (artifact.kind) {
|
|
|
|
.Obj => unreachable,
|
|
|
|
.Test => unreachable,
|
|
|
|
.Exe => .Bin,
|
|
|
|
.Lib => unreachable,
|
|
|
|
},
|
|
|
|
.dest_filename = dest_filename,
|
|
|
|
};
|
|
|
|
self.step.dependOn(&artifact.step);
|
|
|
|
|
|
|
|
builder.pushInstalledFile(self.dest_dir, dest_filename);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make(step: *Step) !void {
|
|
|
|
const self = @fieldParentPtr(Self, "step", step);
|
|
|
|
const builder = self.builder;
|
|
|
|
|
|
|
|
if (self.artifact.target.getObjectFormat() != .elf) {
|
|
|
|
warn("InstallRawStep only works with ELF format.\n", .{});
|
|
|
|
return error.InvalidObjectFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
const full_src_path = self.artifact.getOutputPath();
|
|
|
|
const full_dest_path = builder.getInstallPath(self.dest_dir, self.dest_filename);
|
|
|
|
|
2020-03-04 05:00:42 +00:00
|
|
|
fs.cwd().makePath(builder.getInstallPath(self.dest_dir, "")) catch unreachable;
|
2020-03-11 02:01:58 +00:00
|
|
|
try emitRaw(builder.allocator, full_src_path, full_dest_path);
|
2020-01-21 22:32:00 +00:00
|
|
|
}
|
2020-03-04 05:00:42 +00:00
|
|
|
};
|
2020-06-17 18:24:37 +01:00
|
|
|
|
|
|
|
test "" {
|
|
|
|
std.meta.refAllDecls(InstallRawStep);
|
|
|
|
}
|