33//! and any relocations that may have been emitted.
44//! Think about this as fake in-memory Object file for the Zig module.
55
6+ data : std .ArrayListUnmanaged (u8 ) = .{},
67path : []const u8 ,
78index : File.Index ,
89
@@ -101,6 +102,7 @@ pub fn init(self: *ZigObject, elf_file: *Elf) !void {
101102}
102103
103104pub fn deinit (self : * ZigObject , allocator : Allocator ) void {
105+ self .data .deinit (allocator );
104106 allocator .free (self .path );
105107 self .local_esyms .deinit (allocator );
106108 self .global_esyms .deinit (allocator );
@@ -441,6 +443,27 @@ pub fn markLive(self: *ZigObject, elf_file: *Elf) void {
441443 }
442444}
443445
446+ /// This is just a temporary helper function that allows us to re-read what we wrote to file into a buffer.
447+ /// We need this so that we can write to an archive.
448+ /// TODO implement writing ZigObject data directly to a buffer instead.
449+ pub fn readFileContents (self : * ZigObject , elf_file : * Elf ) ! void {
450+ const gpa = elf_file .base .allocator ;
451+ const shsize : u64 = switch (elf_file .ptr_width ) {
452+ .p32 = > @sizeOf (elf .Elf32_Shdr ),
453+ .p64 = > @sizeOf (elf .Elf64_Shdr ),
454+ };
455+ var end_pos : u64 = elf_file .shdr_table_offset .? + elf_file .shdrs .items .len * shsize ;
456+ for (elf_file .shdrs .items ) | shdr | {
457+ if (shdr .sh_type == elf .SHT_NOBITS ) continue ;
458+ end_pos = @max (end_pos , shdr .sh_offset + shdr .sh_size );
459+ }
460+ const size = std .math .cast (usize , end_pos ) orelse return error .Overflow ;
461+ try self .data .resize (gpa , size );
462+
463+ const amt = try elf_file .base .file .? .preadAll (self .data .items , 0 );
464+ if (amt != size ) return error .InputOutput ;
465+ }
466+
444467pub fn updateArSymtab (self : ZigObject , ar_symtab : * Archive.ArSymtab , elf_file : * Elf ) error {OutOfMemory }! void {
445468 const gpa = elf_file .base .allocator ;
446469
@@ -457,34 +480,21 @@ pub fn updateArSymtab(self: ZigObject, ar_symtab: *Archive.ArSymtab, elf_file: *
457480 }
458481}
459482
460- pub fn updateArSize (self : * ZigObject , elf_file : * Elf ) void {
461- var end_pos : u64 = elf_file .shdr_table_offset .? ;
462- for (elf_file .shdrs .items ) | shdr | {
463- end_pos = @max (end_pos , shdr .sh_offset + shdr .sh_size );
464- }
465- self .output_ar_state .size = end_pos ;
483+ pub fn updateArSize (self : * ZigObject ) void {
484+ self .output_ar_state .size = self .data .items .len ;
466485}
467486
468- pub fn writeAr (self : ZigObject , elf_file : * Elf , writer : anytype ) ! void {
469- const gpa = elf_file .base .allocator ;
470-
471- const size = std .math .cast (usize , self .output_ar_state .size ) orelse return error .Overflow ;
472- const contents = try gpa .alloc (u8 , size );
473- defer gpa .free (contents );
474-
475- const amt = try elf_file .base .file .? .preadAll (contents , 0 );
476- if (amt != self .output_ar_state .size ) return error .InputOutput ;
477-
487+ pub fn writeAr (self : ZigObject , writer : anytype ) ! void {
478488 const name = self .path ;
479489 const hdr = Archive .setArHdr (.{
480490 .name = if (name .len <= Archive .max_member_name_len )
481491 .{ .name = name }
482492 else
483493 .{ .name_off = self .output_ar_state .name_off },
484- .size = @intCast (size ),
494+ .size = @intCast (self . data . items . len ),
485495 });
486496 try writer .writeAll (mem .asBytes (& hdr ));
487- try writer .writeAll (contents );
497+ try writer .writeAll (self . data . items );
488498}
489499
490500pub fn addAtomsToRelaSections (self : ZigObject , elf_file : * Elf ) ! void {
0 commit comments