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...@@ -897,7 +897,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
897 } else null;897 } else null;
898 const gc_sections = self.base.options.gc_sections orelse false;898 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) {
901 // TODO this will become -r route I guess. For now, just copy the object file.901 // TODO this will become -r route I guess. For now, just copy the object file.
902 assert(self.base.file == null); // TODO uncomment once we implement -r902 assert(self.base.file == null); // TODO uncomment once we implement -r
903 const the_object_path = blk: {903 const the_object_path = blk: {
...@@ -1364,7 +1364,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1364,7 +1364,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
13641364
1365 // If we haven't already, create a linker-generated input file comprising of1365 // If we haven't already, create a linker-generated input file comprising of
1366 // linker-defined synthetic symbols only such as `_DYNAMIC`, etc.1366 // 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()) {
1368 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));1368 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));
1369 self.files.set(index, .{ .linker_defined = .{ .index = index } });1369 self.files.set(index, .{ .linker_defined = .{ .index = index } });
1370 self.linker_defined_index = index;1370 self.linker_defined_index = index;
...@@ -1377,6 +1377,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1377,6 +1377,11 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1377 // symbol for potential resolution at load-time.1377 // symbol for potential resolution at load-time.
1378 self.resolveSymbols();1378 self.resolveSymbols();
1379 self.markEhFrameAtomsDead();1379 self.markEhFrameAtomsDead();
1380
1381 if (self.isObject()) {
1382 return self.flushObject(comp);
1383 }
1384
1380 try self.convertCommonSymbols();1385 try self.convertCommonSymbols();
1381 self.markImportsExports();1386 self.markImportsExports();
13821387
...@@ -1470,6 +1475,13 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1470,6 +1475,13 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1470 }1475 }
1471}1476}
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
1473const ParseError = error{1485const ParseError = error{
1474 UnknownFileType,1486 UnknownFileType,
1475 InvalidCpuArch,1487 InvalidCpuArch,
...@@ -2766,7 +2778,7 @@ fn writeHeader(self: *Elf) !void {...@@ -2766,7 +2778,7 @@ fn writeHeader(self: *Elf) !void {
2766 index += 4;2778 index += 4;
27672779
2768 const e_entry = if (self.entry_index) |entry_index| self.symbol(entry_index).value else 0;2780 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;
2770 switch (self.ptr_width) {2782 switch (self.ptr_width) {
2771 .p32 => {2783 .p32 => {
2772 mem.writeInt(u32, hdr_buf[index..][0..4], @as(u32, @intCast(e_entry)), endian);2784 mem.writeInt(u32, hdr_buf[index..][0..4], @as(u32, @intCast(e_entry)), endian);
...@@ -4845,6 +4857,10 @@ pub fn isStatic(self: Elf) bool {...@@ -4845,6 +4857,10 @@ pub fn isStatic(self: Elf) bool {
4845 return self.base.options.link_mode == .Static;4857 return self.base.options.link_mode == .Static;
4846}4858}
48474859
4860pub fn isObject(self: Elf) bool {
4861 return self.base.options.effectiveOutputMode() == .Obj;
4862}
4863
4848pub fn isExe(self: Elf) bool {4864pub fn isExe(self: Elf) bool {
4849 return self.base.options.effectiveOutputMode() == .Exe;4865 return self.base.options.effectiveOutputMode() == .Exe;
4850}4866}