authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-01 15:56:30+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-04 09:08:38+01:00
log1be94878e37c4038a33089b432f99c085361ee0f
tree2ab9c4eb5c2664495893505e0205658dad9d0e92
parentb8c8565e93d3625e5eb46a3f64a461f86b8d8eb4

elf: generate invalid object file but with almost correct header


1 files changed, 19 insertions(+), 3 deletions(-)

src/link/Elf.zig+19-3
......@@ -897,7 +897,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
897897 } else null;
898898 const gc_sections = self.base.options.gc_sections orelse false;
899899
900 if (self.base.options.output_mode == .Obj and self.zig_object_index == null) {
900 if (self.isObject() and self.zig_object_index == null) {
901901 // TODO this will become -r route I guess. For now, just copy the object file.
902902 assert(self.base.file == null); // TODO uncomment once we implement -r
903903 const the_object_path = blk: {
......@@ -1364,7 +1364,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
13641364
13651365 // If we haven't already, create a linker-generated input file comprising of
13661366 // linker-defined synthetic symbols only such as `_DYNAMIC`, etc.
1367 if (self.linker_defined_index == null) {
1367 if (self.linker_defined_index == null and !self.isObject()) {
13681368 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));
13691369 self.files.set(index, .{ .linker_defined = .{ .index = index } });
13701370 self.linker_defined_index = index;
......@@ -1377,6 +1377,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
13771377 // symbol for potential resolution at load-time.
13781378 self.resolveSymbols();
13791379 self.markEhFrameAtomsDead();
1380
1381 if (self.isObject()) {
1382 return self.flushObject(comp);
1383 }
1384
13801385 try self.convertCommonSymbols();
13811386 self.markImportsExports();
13821387
......@@ -1470,6 +1475,13 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
14701475 }
14711476}
14721477
1478pub fn flushObject(self: *Elf, comp: *Compilation) link.File.FlushError!void {
1479 _ = comp;
1480 try self.initSections();
1481 try self.writeShdrTable();
1482 try self.writeHeader();
1483}
1484
14731485const ParseError = error{
14741486 UnknownFileType,
14751487 InvalidCpuArch,
......@@ -2766,7 +2778,7 @@ fn writeHeader(self: *Elf) !void {
27662778 index += 4;
27672779
27682780 const e_entry = if (self.entry_index) |entry_index| self.symbol(entry_index).value else 0;
2769 const phdr_table_offset = self.phdrs.items[self.phdr_table_index.?].p_offset;
2781 const phdr_table_offset = if (self.phdr_table_index) |phndx| self.phdrs.items[phndx].p_offset else 0;
27702782 switch (self.ptr_width) {
27712783 .p32 => {
27722784 mem.writeInt(u32, hdr_buf[index..][0..4], @as(u32, @intCast(e_entry)), endian);
......@@ -4845,6 +4857,10 @@ pub fn isStatic(self: Elf) bool {
48454857 return self.base.options.link_mode == .Static;
48464858}
48474859
4860pub fn isObject(self: Elf) bool {
4861 return self.base.options.effectiveOutputMode() == .Obj;
4862}
4863
48484864pub fn isExe(self: Elf) bool {
48494865 return self.base.options.effectiveOutputMode() == .Exe;
48504866}