| ... | ... | @@ -57,6 +57,7 @@ phdrs: ProgramHeaderList = .empty, |
| 57 | 57 | |
| 58 | 58 | /// Special program headers. |
| 59 | 59 | phdr_indexes: ProgramHeaderIndexes = .{}, |
| 60 | section_indexes: SectionIndexes = .{}, |
| 60 | 61 | |
| 61 | 62 | page_size: u32, |
| 62 | 63 | default_sym_version: elf.Elf64_Versym, |
| ... | ... | @@ -99,29 +100,6 @@ rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .empty, |
| 99 | 100 | /// Applies only to a relocatable. |
| 100 | 101 | comdat_group_sections: std.ArrayListUnmanaged(ComdatGroupSection) = .empty, |
| 101 | 102 | |
| 102 | | copy_rel_section_index: ?u32 = null, |
| 103 | | dynamic_section_index: ?u32 = null, |
| 104 | | dynstrtab_section_index: ?u32 = null, |
| 105 | | dynsymtab_section_index: ?u32 = null, |
| 106 | | eh_frame_section_index: ?u32 = null, |
| 107 | | eh_frame_rela_section_index: ?u32 = null, |
| 108 | | eh_frame_hdr_section_index: ?u32 = null, |
| 109 | | hash_section_index: ?u32 = null, |
| 110 | | gnu_hash_section_index: ?u32 = null, |
| 111 | | got_section_index: ?u32 = null, |
| 112 | | got_plt_section_index: ?u32 = null, |
| 113 | | interp_section_index: ?u32 = null, |
| 114 | | plt_section_index: ?u32 = null, |
| 115 | | plt_got_section_index: ?u32 = null, |
| 116 | | rela_dyn_section_index: ?u32 = null, |
| 117 | | rela_plt_section_index: ?u32 = null, |
| 118 | | versym_section_index: ?u32 = null, |
| 119 | | verneed_section_index: ?u32 = null, |
| 120 | | |
| 121 | | shstrtab_section_index: ?u32 = null, |
| 122 | | strtab_section_index: ?u32 = null, |
| 123 | | symtab_section_index: ?u32 = null, |
| 124 | | |
| 125 | 103 | resolver: SymbolResolver = .{}, |
| 126 | 104 | |
| 127 | 105 | has_text_reloc: bool = false, |
| ... | ... | @@ -136,6 +114,31 @@ comment_merge_section_index: ?MergeSection.Index = null, |
| 136 | 114 | |
| 137 | 115 | first_eflags: ?elf.Elf64_Word = null, |
| 138 | 116 | |
| 117 | const SectionIndexes = struct { |
| 118 | copy_rel: ?u32 = null, |
| 119 | dynamic: ?u32 = null, |
| 120 | dynstrtab: ?u32 = null, |
| 121 | dynsymtab: ?u32 = null, |
| 122 | eh_frame: ?u32 = null, |
| 123 | eh_frame_rela: ?u32 = null, |
| 124 | eh_frame_hdr: ?u32 = null, |
| 125 | hash: ?u32 = null, |
| 126 | gnu_hash: ?u32 = null, |
| 127 | got: ?u32 = null, |
| 128 | got_plt: ?u32 = null, |
| 129 | interp: ?u32 = null, |
| 130 | plt: ?u32 = null, |
| 131 | plt_got: ?u32 = null, |
| 132 | rela_dyn: ?u32 = null, |
| 133 | rela_plt: ?u32 = null, |
| 134 | versym: ?u32 = null, |
| 135 | verneed: ?u32 = null, |
| 136 | |
| 137 | shstrtab: ?u32 = null, |
| 138 | strtab: ?u32 = null, |
| 139 | symtab: ?u32 = null, |
| 140 | }; |
| 141 | |
| 139 | 142 | const ProgramHeaderList = std.ArrayListUnmanaged(elf.Elf64_Phdr); |
| 140 | 143 | |
| 141 | 144 | const OptionalProgramHeaderIndex = enum(u16) { |
| ... | ... | @@ -2670,7 +2673,7 @@ pub fn writeElfHeader(self: *Elf) !void { |
| 2670 | 2673 | mem.writeInt(u16, hdr_buf[index..][0..2], e_shnum, endian); |
| 2671 | 2674 | index += 2; |
| 2672 | 2675 | |
| 2673 | | mem.writeInt(u16, hdr_buf[index..][0..2], @intCast(self.shstrtab_section_index.?), endian); |
| 2676 | mem.writeInt(u16, hdr_buf[index..][0..2], @intCast(self.section_indexes.shstrtab.?), endian); |
| 2674 | 2677 | index += 2; |
| 2675 | 2678 | |
| 2676 | 2679 | assert(index == e_ehsize); |
| ... | ... | @@ -2888,8 +2891,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 2888 | 2891 | } else false; |
| 2889 | 2892 | }; |
| 2890 | 2893 | if (needs_eh_frame) { |
| 2891 | | if (self.eh_frame_section_index == null) { |
| 2892 | | self.eh_frame_section_index = self.sectionByName(".eh_frame") orelse try self.addSection(.{ |
| 2894 | if (self.section_indexes.eh_frame == null) { |
| 2895 | self.section_indexes.eh_frame = self.sectionByName(".eh_frame") orelse try self.addSection(.{ |
| 2893 | 2896 | .name = try self.insertShString(".eh_frame"), |
| 2894 | 2897 | .type = if (target.cpu.arch == .x86_64) |
| 2895 | 2898 | elf.SHT_X86_64_UNWIND |
| ... | ... | @@ -2899,8 +2902,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 2899 | 2902 | .addralign = ptr_size, |
| 2900 | 2903 | }); |
| 2901 | 2904 | } |
| 2902 | | if (comp.link_eh_frame_hdr and self.eh_frame_hdr_section_index == null) { |
| 2903 | | self.eh_frame_hdr_section_index = try self.addSection(.{ |
| 2905 | if (comp.link_eh_frame_hdr and self.section_indexes.eh_frame_hdr == null) { |
| 2906 | self.section_indexes.eh_frame_hdr = try self.addSection(.{ |
| 2904 | 2907 | .name = try self.insertShString(".eh_frame_hdr"), |
| 2905 | 2908 | .type = elf.SHT_PROGBITS, |
| 2906 | 2909 | .flags = elf.SHF_ALLOC, |
| ... | ... | @@ -2909,8 +2912,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 2909 | 2912 | } |
| 2910 | 2913 | } |
| 2911 | 2914 | |
| 2912 | | if (self.got.entries.items.len > 0 and self.got_section_index == null) { |
| 2913 | | self.got_section_index = try self.addSection(.{ |
| 2915 | if (self.got.entries.items.len > 0 and self.section_indexes.got == null) { |
| 2916 | self.section_indexes.got = try self.addSection(.{ |
| 2914 | 2917 | .name = try self.insertShString(".got"), |
| 2915 | 2918 | .type = elf.SHT_PROGBITS, |
| 2916 | 2919 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| ... | ... | @@ -2918,8 +2921,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 2918 | 2921 | }); |
| 2919 | 2922 | } |
| 2920 | 2923 | |
| 2921 | | if (self.got_plt_section_index == null) { |
| 2922 | | self.got_plt_section_index = try self.addSection(.{ |
| 2924 | if (self.section_indexes.got_plt == null) { |
| 2925 | self.section_indexes.got_plt = try self.addSection(.{ |
| 2923 | 2926 | .name = try self.insertShString(".got.plt"), |
| 2924 | 2927 | .type = elf.SHT_PROGBITS, |
| 2925 | 2928 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| ... | ... | @@ -2938,8 +2941,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 2938 | 2941 | } |
| 2939 | 2942 | break :blk false; |
| 2940 | 2943 | }; |
| 2941 | | if (needs_rela_dyn and self.rela_dyn_section_index == null) { |
| 2942 | | self.rela_dyn_section_index = try self.addSection(.{ |
| 2944 | if (needs_rela_dyn and self.section_indexes.rela_dyn == null) { |
| 2945 | self.section_indexes.rela_dyn = try self.addSection(.{ |
| 2943 | 2946 | .name = try self.insertShString(".rela.dyn"), |
| 2944 | 2947 | .type = elf.SHT_RELA, |
| 2945 | 2948 | .flags = elf.SHF_ALLOC, |
| ... | ... | @@ -2949,16 +2952,16 @@ fn initSyntheticSections(self: *Elf) !void { |
| 2949 | 2952 | } |
| 2950 | 2953 | |
| 2951 | 2954 | if (self.plt.symbols.items.len > 0) { |
| 2952 | | if (self.plt_section_index == null) { |
| 2953 | | self.plt_section_index = try self.addSection(.{ |
| 2955 | if (self.section_indexes.plt == null) { |
| 2956 | self.section_indexes.plt = try self.addSection(.{ |
| 2954 | 2957 | .name = try self.insertShString(".plt"), |
| 2955 | 2958 | .type = elf.SHT_PROGBITS, |
| 2956 | 2959 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| 2957 | 2960 | .addralign = 16, |
| 2958 | 2961 | }); |
| 2959 | 2962 | } |
| 2960 | | if (self.rela_plt_section_index == null) { |
| 2961 | | self.rela_plt_section_index = try self.addSection(.{ |
| 2963 | if (self.section_indexes.rela_plt == null) { |
| 2964 | self.section_indexes.rela_plt = try self.addSection(.{ |
| 2962 | 2965 | .name = try self.insertShString(".rela.plt"), |
| 2963 | 2966 | .type = elf.SHT_RELA, |
| 2964 | 2967 | .flags = elf.SHF_ALLOC, |
| ... | ... | @@ -2968,8 +2971,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 2968 | 2971 | } |
| 2969 | 2972 | } |
| 2970 | 2973 | |
| 2971 | | if (self.plt_got.symbols.items.len > 0 and self.plt_got_section_index == null) { |
| 2972 | | self.plt_got_section_index = try self.addSection(.{ |
| 2974 | if (self.plt_got.symbols.items.len > 0 and self.section_indexes.plt_got == null) { |
| 2975 | self.section_indexes.plt_got = try self.addSection(.{ |
| 2973 | 2976 | .name = try self.insertShString(".plt.got"), |
| 2974 | 2977 | .type = elf.SHT_PROGBITS, |
| 2975 | 2978 | .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR, |
| ... | ... | @@ -2977,8 +2980,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 2977 | 2980 | }); |
| 2978 | 2981 | } |
| 2979 | 2982 | |
| 2980 | | if (self.copy_rel.symbols.items.len > 0 and self.copy_rel_section_index == null) { |
| 2981 | | self.copy_rel_section_index = try self.addSection(.{ |
| 2983 | if (self.copy_rel.symbols.items.len > 0 and self.section_indexes.copy_rel == null) { |
| 2984 | self.section_indexes.copy_rel = try self.addSection(.{ |
| 2982 | 2985 | .name = try self.insertShString(".copyrel"), |
| 2983 | 2986 | .type = elf.SHT_NOBITS, |
| 2984 | 2987 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| ... | ... | @@ -2994,8 +2997,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 2994 | 2997 | if (self.base.isStatic() and !comp.config.pie) break :blk false; |
| 2995 | 2998 | break :blk target.dynamic_linker.get() != null; |
| 2996 | 2999 | }; |
| 2997 | | if (needs_interp and self.interp_section_index == null) { |
| 2998 | | self.interp_section_index = try self.addSection(.{ |
| 3000 | if (needs_interp and self.section_indexes.interp == null) { |
| 3001 | self.section_indexes.interp = try self.addSection(.{ |
| 2999 | 3002 | .name = try self.insertShString(".interp"), |
| 3000 | 3003 | .type = elf.SHT_PROGBITS, |
| 3001 | 3004 | .flags = elf.SHF_ALLOC, |
| ... | ... | @@ -3004,8 +3007,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 3004 | 3007 | } |
| 3005 | 3008 | |
| 3006 | 3009 | if (self.isEffectivelyDynLib() or self.shared_objects.items.len > 0 or comp.config.pie) { |
| 3007 | | if (self.dynstrtab_section_index == null) { |
| 3008 | | self.dynstrtab_section_index = try self.addSection(.{ |
| 3010 | if (self.section_indexes.dynstrtab == null) { |
| 3011 | self.section_indexes.dynstrtab = try self.addSection(.{ |
| 3009 | 3012 | .name = try self.insertShString(".dynstr"), |
| 3010 | 3013 | .flags = elf.SHF_ALLOC, |
| 3011 | 3014 | .type = elf.SHT_STRTAB, |
| ... | ... | @@ -3013,8 +3016,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 3013 | 3016 | .addralign = 1, |
| 3014 | 3017 | }); |
| 3015 | 3018 | } |
| 3016 | | if (self.dynamic_section_index == null) { |
| 3017 | | self.dynamic_section_index = try self.addSection(.{ |
| 3019 | if (self.section_indexes.dynamic == null) { |
| 3020 | self.section_indexes.dynamic = try self.addSection(.{ |
| 3018 | 3021 | .name = try self.insertShString(".dynamic"), |
| 3019 | 3022 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 3020 | 3023 | .type = elf.SHT_DYNAMIC, |
| ... | ... | @@ -3022,8 +3025,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 3022 | 3025 | .addralign = @alignOf(elf.Elf64_Dyn), |
| 3023 | 3026 | }); |
| 3024 | 3027 | } |
| 3025 | | if (self.dynsymtab_section_index == null) { |
| 3026 | | self.dynsymtab_section_index = try self.addSection(.{ |
| 3028 | if (self.section_indexes.dynsymtab == null) { |
| 3029 | self.section_indexes.dynsymtab = try self.addSection(.{ |
| 3027 | 3030 | .name = try self.insertShString(".dynsym"), |
| 3028 | 3031 | .flags = elf.SHF_ALLOC, |
| 3029 | 3032 | .type = elf.SHT_DYNSYM, |
| ... | ... | @@ -3032,8 +3035,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 3032 | 3035 | .info = 1, |
| 3033 | 3036 | }); |
| 3034 | 3037 | } |
| 3035 | | if (self.hash_section_index == null) { |
| 3036 | | self.hash_section_index = try self.addSection(.{ |
| 3038 | if (self.section_indexes.hash == null) { |
| 3039 | self.section_indexes.hash = try self.addSection(.{ |
| 3037 | 3040 | .name = try self.insertShString(".hash"), |
| 3038 | 3041 | .flags = elf.SHF_ALLOC, |
| 3039 | 3042 | .type = elf.SHT_HASH, |
| ... | ... | @@ -3041,8 +3044,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 3041 | 3044 | .entsize = 4, |
| 3042 | 3045 | }); |
| 3043 | 3046 | } |
| 3044 | | if (self.gnu_hash_section_index == null) { |
| 3045 | | self.gnu_hash_section_index = try self.addSection(.{ |
| 3047 | if (self.section_indexes.gnu_hash == null) { |
| 3048 | self.section_indexes.gnu_hash = try self.addSection(.{ |
| 3046 | 3049 | .name = try self.insertShString(".gnu.hash"), |
| 3047 | 3050 | .flags = elf.SHF_ALLOC, |
| 3048 | 3051 | .type = elf.SHT_GNU_HASH, |
| ... | ... | @@ -3055,8 +3058,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 3055 | 3058 | if (sym.flags.import and sym.version_index & elf.VERSYM_VERSION > elf.VER_NDX_GLOBAL) break true; |
| 3056 | 3059 | } else false; |
| 3057 | 3060 | if (needs_versions) { |
| 3058 | | if (self.versym_section_index == null) { |
| 3059 | | self.versym_section_index = try self.addSection(.{ |
| 3061 | if (self.section_indexes.versym == null) { |
| 3062 | self.section_indexes.versym = try self.addSection(.{ |
| 3060 | 3063 | .name = try self.insertShString(".gnu.version"), |
| 3061 | 3064 | .flags = elf.SHF_ALLOC, |
| 3062 | 3065 | .type = elf.SHT_GNU_VERSYM, |
| ... | ... | @@ -3064,8 +3067,8 @@ fn initSyntheticSections(self: *Elf) !void { |
| 3064 | 3067 | .entsize = @sizeOf(elf.Elf64_Versym), |
| 3065 | 3068 | }); |
| 3066 | 3069 | } |
| 3067 | | if (self.verneed_section_index == null) { |
| 3068 | | self.verneed_section_index = try self.addSection(.{ |
| 3070 | if (self.section_indexes.verneed == null) { |
| 3071 | self.section_indexes.verneed = try self.addSection(.{ |
| 3069 | 3072 | .name = try self.insertShString(".gnu.version_r"), |
| 3070 | 3073 | .flags = elf.SHF_ALLOC, |
| 3071 | 3074 | .type = elf.SHT_GNU_VERNEED, |
| ... | ... | @@ -3084,16 +3087,16 @@ pub fn initSymtab(self: *Elf) !void { |
| 3084 | 3087 | .p32 => true, |
| 3085 | 3088 | .p64 => false, |
| 3086 | 3089 | }; |
| 3087 | | if (self.symtab_section_index == null) { |
| 3088 | | self.symtab_section_index = try self.addSection(.{ |
| 3090 | if (self.section_indexes.symtab == null) { |
| 3091 | self.section_indexes.symtab = try self.addSection(.{ |
| 3089 | 3092 | .name = try self.insertShString(".symtab"), |
| 3090 | 3093 | .type = elf.SHT_SYMTAB, |
| 3091 | 3094 | .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym), |
| 3092 | 3095 | .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym), |
| 3093 | 3096 | }); |
| 3094 | 3097 | } |
| 3095 | | if (self.strtab_section_index == null) { |
| 3096 | | self.strtab_section_index = try self.addSection(.{ |
| 3098 | if (self.section_indexes.strtab == null) { |
| 3099 | self.section_indexes.strtab = try self.addSection(.{ |
| 3097 | 3100 | .name = try self.insertShString(".strtab"), |
| 3098 | 3101 | .type = elf.SHT_STRTAB, |
| 3099 | 3102 | .entsize = 1, |
| ... | ... | @@ -3103,8 +3106,8 @@ pub fn initSymtab(self: *Elf) !void { |
| 3103 | 3106 | } |
| 3104 | 3107 | |
| 3105 | 3108 | pub fn initShStrtab(self: *Elf) !void { |
| 3106 | | if (self.shstrtab_section_index == null) { |
| 3107 | | self.shstrtab_section_index = try self.addSection(.{ |
| 3109 | if (self.section_indexes.shstrtab == null) { |
| 3110 | self.section_indexes.shstrtab = try self.addSection(.{ |
| 3108 | 3111 | .name = try self.insertShString(".shstrtab"), |
| 3109 | 3112 | .type = elf.SHT_STRTAB, |
| 3110 | 3113 | .entsize = 1, |
| ... | ... | @@ -3116,20 +3119,20 @@ pub fn initShStrtab(self: *Elf) !void { |
| 3116 | 3119 | fn initSpecialPhdrs(self: *Elf) !void { |
| 3117 | 3120 | comptime assert(max_number_of_special_phdrs == 5); |
| 3118 | 3121 | |
| 3119 | | if (self.interp_section_index != null and self.phdr_indexes.interp == .none) { |
| 3122 | if (self.section_indexes.interp != null and self.phdr_indexes.interp == .none) { |
| 3120 | 3123 | self.phdr_indexes.interp = (try self.addPhdr(.{ |
| 3121 | 3124 | .type = elf.PT_INTERP, |
| 3122 | 3125 | .flags = elf.PF_R, |
| 3123 | 3126 | .@"align" = 1, |
| 3124 | 3127 | })).toOptional(); |
| 3125 | 3128 | } |
| 3126 | | if (self.dynamic_section_index != null and self.phdr_indexes.dynamic == .none) { |
| 3129 | if (self.section_indexes.dynamic != null and self.phdr_indexes.dynamic == .none) { |
| 3127 | 3130 | self.phdr_indexes.dynamic = (try self.addPhdr(.{ |
| 3128 | 3131 | .type = elf.PT_DYNAMIC, |
| 3129 | 3132 | .flags = elf.PF_R | elf.PF_W, |
| 3130 | 3133 | })).toOptional(); |
| 3131 | 3134 | } |
| 3132 | | if (self.eh_frame_hdr_section_index != null and self.phdr_indexes.gnu_eh_frame == .none) { |
| 3135 | if (self.section_indexes.eh_frame_hdr != null and self.phdr_indexes.gnu_eh_frame == .none) { |
| 3133 | 3136 | self.phdr_indexes.gnu_eh_frame = (try self.addPhdr(.{ |
| 3134 | 3137 | .type = elf.PT_GNU_EH_FRAME, |
| 3135 | 3138 | .flags = elf.PF_R, |
| ... | ... | @@ -3237,7 +3240,7 @@ fn sortInitFini(self: *Elf) !void { |
| 3237 | 3240 | } |
| 3238 | 3241 | |
| 3239 | 3242 | fn setDynamicSection(self: *Elf, rpaths: []const []const u8) !void { |
| 3240 | | if (self.dynamic_section_index == null) return; |
| 3243 | if (self.section_indexes.dynamic == null) return; |
| 3241 | 3244 | |
| 3242 | 3245 | for (self.shared_objects.items) |index| { |
| 3243 | 3246 | const shared_object = self.file(index).?.shared_object; |
| ... | ... | @@ -3255,13 +3258,13 @@ fn setDynamicSection(self: *Elf, rpaths: []const []const u8) !void { |
| 3255 | 3258 | } |
| 3256 | 3259 | |
| 3257 | 3260 | fn sortDynamicSymtab(self: *Elf) void { |
| 3258 | | if (self.gnu_hash_section_index == null) return; |
| 3261 | if (self.section_indexes.gnu_hash == null) return; |
| 3259 | 3262 | self.dynsym.sort(self); |
| 3260 | 3263 | } |
| 3261 | 3264 | |
| 3262 | 3265 | fn setVersionSymtab(self: *Elf) !void { |
| 3263 | 3266 | const gpa = self.base.comp.gpa; |
| 3264 | | if (self.versym_section_index == null) return; |
| 3267 | if (self.section_indexes.versym == null) return; |
| 3265 | 3268 | try self.versym.resize(gpa, self.dynsym.count()); |
| 3266 | 3269 | self.versym.items[0] = elf.VER_NDX_LOCAL; |
| 3267 | 3270 | for (self.dynsym.entries.items, 1..) |entry, i| { |
| ... | ... | @@ -3269,7 +3272,7 @@ fn setVersionSymtab(self: *Elf) !void { |
| 3269 | 3272 | self.versym.items[i] = sym.version_index; |
| 3270 | 3273 | } |
| 3271 | 3274 | |
| 3272 | | if (self.verneed_section_index) |shndx| { |
| 3275 | if (self.section_indexes.verneed) |shndx| { |
| 3273 | 3276 | try self.verneed.generate(self); |
| 3274 | 3277 | const shdr = &self.sections.items(.shdr)[shndx]; |
| 3275 | 3278 | shdr.sh_info = @as(u32, @intCast(self.verneed.verneed.items.len)); |
| ... | ... | @@ -3277,10 +3280,10 @@ fn setVersionSymtab(self: *Elf) !void { |
| 3277 | 3280 | } |
| 3278 | 3281 | |
| 3279 | 3282 | fn setHashSections(self: *Elf) !void { |
| 3280 | | if (self.hash_section_index != null) { |
| 3283 | if (self.section_indexes.hash != null) { |
| 3281 | 3284 | try self.hash.generate(self); |
| 3282 | 3285 | } |
| 3283 | | if (self.gnu_hash_section_index != null) { |
| 3286 | if (self.section_indexes.gnu_hash != null) { |
| 3284 | 3287 | try self.gnu_hash.calcSize(self); |
| 3285 | 3288 | } |
| 3286 | 3289 | } |
| ... | ... | @@ -3420,47 +3423,22 @@ pub fn sortShdrs(self: *Elf) !void { |
| 3420 | 3423 | |
| 3421 | 3424 | const backlinks = try gpa.alloc(u32, entries.items.len); |
| 3422 | 3425 | defer gpa.free(backlinks); |
| 3423 | | for (entries.items, 0..) |entry, i| { |
| 3424 | | backlinks[entry.shndx] = @intCast(i); |
| 3425 | | } |
| 3426 | | |
| 3427 | | var slice = self.sections.toOwnedSlice(); |
| 3428 | | defer slice.deinit(gpa); |
| 3429 | | |
| 3430 | | try self.sections.ensureTotalCapacity(gpa, slice.len); |
| 3431 | | for (entries.items) |sorted| { |
| 3432 | | self.sections.appendAssumeCapacity(slice.get(sorted.shndx)); |
| 3433 | | } |
| 3434 | | |
| 3435 | | self.resetShdrIndexes(backlinks); |
| 3436 | | } |
| 3437 | | |
| 3438 | | fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void { |
| 3439 | | for (&[_]*?u32{ |
| 3440 | | &self.eh_frame_section_index, |
| 3441 | | &self.eh_frame_rela_section_index, |
| 3442 | | &self.eh_frame_hdr_section_index, |
| 3443 | | &self.got_section_index, |
| 3444 | | &self.symtab_section_index, |
| 3445 | | &self.strtab_section_index, |
| 3446 | | &self.shstrtab_section_index, |
| 3447 | | &self.interp_section_index, |
| 3448 | | &self.dynamic_section_index, |
| 3449 | | &self.dynsymtab_section_index, |
| 3450 | | &self.dynstrtab_section_index, |
| 3451 | | &self.hash_section_index, |
| 3452 | | &self.gnu_hash_section_index, |
| 3453 | | &self.plt_section_index, |
| 3454 | | &self.got_plt_section_index, |
| 3455 | | &self.plt_got_section_index, |
| 3456 | | &self.rela_dyn_section_index, |
| 3457 | | &self.rela_plt_section_index, |
| 3458 | | &self.copy_rel_section_index, |
| 3459 | | &self.versym_section_index, |
| 3460 | | &self.verneed_section_index, |
| 3461 | | }) |maybe_index| { |
| 3462 | | if (maybe_index.*) |*index| { |
| 3463 | | index.* = backlinks[index.*]; |
| 3426 | { |
| 3427 | var slice = self.sections.toOwnedSlice(); |
| 3428 | defer slice.deinit(gpa); |
| 3429 | try self.sections.resize(gpa, slice.len); |
| 3430 | |
| 3431 | for (entries.items, 0..) |entry, i| { |
| 3432 | backlinks[entry.shndx] = @intCast(i); |
| 3433 | self.sections.set(i, slice.get(entry.shndx)); |
| 3434 | } |
| 3435 | } |
| 3436 | |
| 3437 | const special_indexes = &self.section_indexes; |
| 3438 | |
| 3439 | inline for (@typeInfo(SectionIndexes).@"struct".fields) |field| { |
| 3440 | if (@field(special_indexes, field.name)) |special_index| { |
| 3441 | @field(special_indexes, field.name) = backlinks[special_index]; |
| 3464 | 3442 | } |
| 3465 | 3443 | } |
| 3466 | 3444 | |
| ... | ... | @@ -3478,7 +3456,7 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void { |
| 3478 | 3456 | // FIXME:JK we should spin up .symtab potentially earlier, or set all non-dynamic RELA sections |
| 3479 | 3457 | // to point at symtab |
| 3480 | 3458 | // shdr.sh_link = backlinks[shdr.sh_link]; |
| 3481 | | shdr.sh_link = self.symtab_section_index.?; |
| 3459 | shdr.sh_link = self.section_indexes.symtab.?; |
| 3482 | 3460 | shdr.sh_info = backlinks[shdr.sh_info]; |
| 3483 | 3461 | } |
| 3484 | 3462 | } |
| ... | ... | @@ -3489,56 +3467,56 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void { |
| 3489 | 3467 | cg.shndx = backlinks[cg.shndx]; |
| 3490 | 3468 | } |
| 3491 | 3469 | |
| 3492 | | if (self.symtab_section_index) |index| { |
| 3470 | if (self.section_indexes.symtab) |index| { |
| 3493 | 3471 | const shdr = &slice.items(.shdr)[index]; |
| 3494 | | shdr.sh_link = self.strtab_section_index.?; |
| 3472 | shdr.sh_link = self.section_indexes.strtab.?; |
| 3495 | 3473 | } |
| 3496 | 3474 | |
| 3497 | | if (self.dynamic_section_index) |index| { |
| 3475 | if (self.section_indexes.dynamic) |index| { |
| 3498 | 3476 | const shdr = &slice.items(.shdr)[index]; |
| 3499 | | shdr.sh_link = self.dynstrtab_section_index.?; |
| 3477 | shdr.sh_link = self.section_indexes.dynstrtab.?; |
| 3500 | 3478 | } |
| 3501 | 3479 | |
| 3502 | | if (self.dynsymtab_section_index) |index| { |
| 3480 | if (self.section_indexes.dynsymtab) |index| { |
| 3503 | 3481 | const shdr = &slice.items(.shdr)[index]; |
| 3504 | | shdr.sh_link = self.dynstrtab_section_index.?; |
| 3482 | shdr.sh_link = self.section_indexes.dynstrtab.?; |
| 3505 | 3483 | } |
| 3506 | 3484 | |
| 3507 | | if (self.hash_section_index) |index| { |
| 3485 | if (self.section_indexes.hash) |index| { |
| 3508 | 3486 | const shdr = &slice.items(.shdr)[index]; |
| 3509 | | shdr.sh_link = self.dynsymtab_section_index.?; |
| 3487 | shdr.sh_link = self.section_indexes.dynsymtab.?; |
| 3510 | 3488 | } |
| 3511 | 3489 | |
| 3512 | | if (self.gnu_hash_section_index) |index| { |
| 3490 | if (self.section_indexes.gnu_hash) |index| { |
| 3513 | 3491 | const shdr = &slice.items(.shdr)[index]; |
| 3514 | | shdr.sh_link = self.dynsymtab_section_index.?; |
| 3492 | shdr.sh_link = self.section_indexes.dynsymtab.?; |
| 3515 | 3493 | } |
| 3516 | 3494 | |
| 3517 | | if (self.versym_section_index) |index| { |
| 3495 | if (self.section_indexes.versym) |index| { |
| 3518 | 3496 | const shdr = &slice.items(.shdr)[index]; |
| 3519 | | shdr.sh_link = self.dynsymtab_section_index.?; |
| 3497 | shdr.sh_link = self.section_indexes.dynsymtab.?; |
| 3520 | 3498 | } |
| 3521 | 3499 | |
| 3522 | | if (self.verneed_section_index) |index| { |
| 3500 | if (self.section_indexes.verneed) |index| { |
| 3523 | 3501 | const shdr = &slice.items(.shdr)[index]; |
| 3524 | | shdr.sh_link = self.dynstrtab_section_index.?; |
| 3502 | shdr.sh_link = self.section_indexes.dynstrtab.?; |
| 3525 | 3503 | } |
| 3526 | 3504 | |
| 3527 | | if (self.rela_dyn_section_index) |index| { |
| 3505 | if (self.section_indexes.rela_dyn) |index| { |
| 3528 | 3506 | const shdr = &slice.items(.shdr)[index]; |
| 3529 | | shdr.sh_link = self.dynsymtab_section_index orelse 0; |
| 3507 | shdr.sh_link = self.section_indexes.dynsymtab orelse 0; |
| 3530 | 3508 | } |
| 3531 | 3509 | |
| 3532 | | if (self.rela_plt_section_index) |index| { |
| 3510 | if (self.section_indexes.rela_plt) |index| { |
| 3533 | 3511 | const shdr = &slice.items(.shdr)[index]; |
| 3534 | | shdr.sh_link = self.dynsymtab_section_index.?; |
| 3535 | | shdr.sh_info = self.plt_section_index.?; |
| 3512 | shdr.sh_link = self.section_indexes.dynsymtab.?; |
| 3513 | shdr.sh_info = self.section_indexes.plt.?; |
| 3536 | 3514 | } |
| 3537 | 3515 | |
| 3538 | | if (self.eh_frame_rela_section_index) |index| { |
| 3516 | if (self.section_indexes.eh_frame_rela) |index| { |
| 3539 | 3517 | const shdr = &slice.items(.shdr)[index]; |
| 3540 | | shdr.sh_link = self.symtab_section_index.?; |
| 3541 | | shdr.sh_info = self.eh_frame_section_index.?; |
| 3518 | shdr.sh_link = self.section_indexes.symtab.?; |
| 3519 | shdr.sh_info = self.section_indexes.eh_frame.?; |
| 3542 | 3520 | } |
| 3543 | 3521 | } |
| 3544 | 3522 | |
| ... | ... | @@ -3572,31 +3550,31 @@ fn updateSectionSizes(self: *Elf) !void { |
| 3572 | 3550 | } |
| 3573 | 3551 | |
| 3574 | 3552 | const shdrs = slice.items(.shdr); |
| 3575 | | if (self.eh_frame_section_index) |index| { |
| 3553 | if (self.section_indexes.eh_frame) |index| { |
| 3576 | 3554 | shdrs[index].sh_size = try eh_frame.calcEhFrameSize(self); |
| 3577 | 3555 | } |
| 3578 | 3556 | |
| 3579 | | if (self.eh_frame_hdr_section_index) |index| { |
| 3557 | if (self.section_indexes.eh_frame_hdr) |index| { |
| 3580 | 3558 | shdrs[index].sh_size = eh_frame.calcEhFrameHdrSize(self); |
| 3581 | 3559 | } |
| 3582 | 3560 | |
| 3583 | | if (self.got_section_index) |index| { |
| 3561 | if (self.section_indexes.got) |index| { |
| 3584 | 3562 | shdrs[index].sh_size = self.got.size(self); |
| 3585 | 3563 | } |
| 3586 | 3564 | |
| 3587 | | if (self.plt_section_index) |index| { |
| 3565 | if (self.section_indexes.plt) |index| { |
| 3588 | 3566 | shdrs[index].sh_size = self.plt.size(self); |
| 3589 | 3567 | } |
| 3590 | 3568 | |
| 3591 | | if (self.got_plt_section_index) |index| { |
| 3569 | if (self.section_indexes.got_plt) |index| { |
| 3592 | 3570 | shdrs[index].sh_size = self.got_plt.size(self); |
| 3593 | 3571 | } |
| 3594 | 3572 | |
| 3595 | | if (self.plt_got_section_index) |index| { |
| 3573 | if (self.section_indexes.plt_got) |index| { |
| 3596 | 3574 | shdrs[index].sh_size = self.plt_got.size(self); |
| 3597 | 3575 | } |
| 3598 | 3576 | |
| 3599 | | if (self.rela_dyn_section_index) |shndx| { |
| 3577 | if (self.section_indexes.rela_dyn) |shndx| { |
| 3600 | 3578 | var num = self.got.numRela(self) + self.copy_rel.numRela(); |
| 3601 | 3579 | if (self.zigObjectPtr()) |zig_object| { |
| 3602 | 3580 | num += zig_object.num_dynrelocs; |
| ... | ... | @@ -3607,43 +3585,43 @@ fn updateSectionSizes(self: *Elf) !void { |
| 3607 | 3585 | shdrs[shndx].sh_size = num * @sizeOf(elf.Elf64_Rela); |
| 3608 | 3586 | } |
| 3609 | 3587 | |
| 3610 | | if (self.rela_plt_section_index) |index| { |
| 3588 | if (self.section_indexes.rela_plt) |index| { |
| 3611 | 3589 | shdrs[index].sh_size = self.plt.numRela() * @sizeOf(elf.Elf64_Rela); |
| 3612 | 3590 | } |
| 3613 | 3591 | |
| 3614 | | if (self.copy_rel_section_index) |index| { |
| 3592 | if (self.section_indexes.copy_rel) |index| { |
| 3615 | 3593 | try self.copy_rel.updateSectionSize(index, self); |
| 3616 | 3594 | } |
| 3617 | 3595 | |
| 3618 | | if (self.interp_section_index) |index| { |
| 3596 | if (self.section_indexes.interp) |index| { |
| 3619 | 3597 | shdrs[index].sh_size = self.getTarget().dynamic_linker.get().?.len + 1; |
| 3620 | 3598 | } |
| 3621 | 3599 | |
| 3622 | | if (self.hash_section_index) |index| { |
| 3600 | if (self.section_indexes.hash) |index| { |
| 3623 | 3601 | shdrs[index].sh_size = self.hash.size(); |
| 3624 | 3602 | } |
| 3625 | 3603 | |
| 3626 | | if (self.gnu_hash_section_index) |index| { |
| 3604 | if (self.section_indexes.gnu_hash) |index| { |
| 3627 | 3605 | shdrs[index].sh_size = self.gnu_hash.size(); |
| 3628 | 3606 | } |
| 3629 | 3607 | |
| 3630 | | if (self.dynamic_section_index) |index| { |
| 3608 | if (self.section_indexes.dynamic) |index| { |
| 3631 | 3609 | shdrs[index].sh_size = self.dynamic.size(self); |
| 3632 | 3610 | } |
| 3633 | 3611 | |
| 3634 | | if (self.dynsymtab_section_index) |index| { |
| 3612 | if (self.section_indexes.dynsymtab) |index| { |
| 3635 | 3613 | shdrs[index].sh_size = self.dynsym.size(); |
| 3636 | 3614 | } |
| 3637 | 3615 | |
| 3638 | | if (self.dynstrtab_section_index) |index| { |
| 3616 | if (self.section_indexes.dynstrtab) |index| { |
| 3639 | 3617 | shdrs[index].sh_size = self.dynstrtab.items.len; |
| 3640 | 3618 | } |
| 3641 | 3619 | |
| 3642 | | if (self.versym_section_index) |index| { |
| 3620 | if (self.section_indexes.versym) |index| { |
| 3643 | 3621 | shdrs[index].sh_size = self.versym.items.len * @sizeOf(elf.Elf64_Versym); |
| 3644 | 3622 | } |
| 3645 | 3623 | |
| 3646 | | if (self.verneed_section_index) |index| { |
| 3624 | if (self.section_indexes.verneed) |index| { |
| 3647 | 3625 | shdrs[index].sh_size = self.verneed.size(); |
| 3648 | 3626 | } |
| 3649 | 3627 | |
| ... | ... | @@ -3653,7 +3631,7 @@ fn updateSectionSizes(self: *Elf) !void { |
| 3653 | 3631 | |
| 3654 | 3632 | // FIXME:JK this is very much obsolete, remove! |
| 3655 | 3633 | pub fn updateShStrtabSize(self: *Elf) void { |
| 3656 | | if (self.shstrtab_section_index) |index| { |
| 3634 | if (self.section_indexes.shstrtab) |index| { |
| 3657 | 3635 | self.sections.items(.shdr)[index].sh_size = self.shstrtab.items.len; |
| 3658 | 3636 | } |
| 3659 | 3637 | } |
| ... | ... | @@ -3936,9 +3914,9 @@ fn allocateSpecialPhdrs(self: *Elf) void { |
| 3936 | 3914 | const slice = self.sections.slice(); |
| 3937 | 3915 | |
| 3938 | 3916 | for (&[_]struct { OptionalProgramHeaderIndex, ?u32 }{ |
| 3939 | | .{ self.phdr_indexes.interp, self.interp_section_index }, |
| 3940 | | .{ self.phdr_indexes.dynamic, self.dynamic_section_index }, |
| 3941 | | .{ self.phdr_indexes.gnu_eh_frame, self.eh_frame_hdr_section_index }, |
| 3917 | .{ self.phdr_indexes.interp, self.section_indexes.interp }, |
| 3918 | .{ self.phdr_indexes.dynamic, self.section_indexes.dynamic }, |
| 3919 | .{ self.phdr_indexes.gnu_eh_frame, self.section_indexes.eh_frame_hdr }, |
| 3942 | 3920 | }) |pair| { |
| 3943 | 3921 | if (pair[0].int()) |index| { |
| 3944 | 3922 | const shdr = slice.items(.shdr)[pair[1].?]; |
| ... | ... | @@ -4075,7 +4053,7 @@ pub fn updateSymtabSize(self: *Elf) !void { |
| 4075 | 4053 | strsize += ctx.strsize; |
| 4076 | 4054 | } |
| 4077 | 4055 | |
| 4078 | | if (self.got_section_index) |_| { |
| 4056 | if (self.section_indexes.got) |_| { |
| 4079 | 4057 | self.got.output_symtab_ctx.reset(); |
| 4080 | 4058 | self.got.output_symtab_ctx.ilocal = nlocals; |
| 4081 | 4059 | self.got.updateSymtabSize(self); |
| ... | ... | @@ -4083,7 +4061,7 @@ pub fn updateSymtabSize(self: *Elf) !void { |
| 4083 | 4061 | strsize += self.got.output_symtab_ctx.strsize; |
| 4084 | 4062 | } |
| 4085 | 4063 | |
| 4086 | | if (self.plt_section_index) |_| { |
| 4064 | if (self.section_indexes.plt) |_| { |
| 4087 | 4065 | self.plt.output_symtab_ctx.reset(); |
| 4088 | 4066 | self.plt.output_symtab_ctx.ilocal = nlocals; |
| 4089 | 4067 | self.plt.updateSymtabSize(self); |
| ... | ... | @@ -4091,7 +4069,7 @@ pub fn updateSymtabSize(self: *Elf) !void { |
| 4091 | 4069 | strsize += self.plt.output_symtab_ctx.strsize; |
| 4092 | 4070 | } |
| 4093 | 4071 | |
| 4094 | | if (self.plt_got_section_index) |_| { |
| 4072 | if (self.section_indexes.plt_got) |_| { |
| 4095 | 4073 | self.plt_got.output_symtab_ctx.reset(); |
| 4096 | 4074 | self.plt_got.output_symtab_ctx.ilocal = nlocals; |
| 4097 | 4075 | self.plt_got.updateSymtabSize(self); |
| ... | ... | @@ -4108,9 +4086,9 @@ pub fn updateSymtabSize(self: *Elf) !void { |
| 4108 | 4086 | } |
| 4109 | 4087 | |
| 4110 | 4088 | const slice = self.sections.slice(); |
| 4111 | | const symtab_shdr = &slice.items(.shdr)[self.symtab_section_index.?]; |
| 4089 | const symtab_shdr = &slice.items(.shdr)[self.section_indexes.symtab.?]; |
| 4112 | 4090 | symtab_shdr.sh_info = nlocals; |
| 4113 | | symtab_shdr.sh_link = self.strtab_section_index.?; |
| 4091 | symtab_shdr.sh_link = self.section_indexes.strtab.?; |
| 4114 | 4092 | |
| 4115 | 4093 | const sym_size: u64 = switch (self.ptr_width) { |
| 4116 | 4094 | .p32 => @sizeOf(elf.Elf32_Sym), |
| ... | ... | @@ -4119,7 +4097,7 @@ pub fn updateSymtabSize(self: *Elf) !void { |
| 4119 | 4097 | const needed_size = (nlocals + nglobals) * sym_size; |
| 4120 | 4098 | symtab_shdr.sh_size = needed_size; |
| 4121 | 4099 | |
| 4122 | | const strtab = &slice.items(.shdr)[self.strtab_section_index.?]; |
| 4100 | const strtab = &slice.items(.shdr)[self.section_indexes.strtab.?]; |
| 4123 | 4101 | strtab.sh_size = strsize + 1; |
| 4124 | 4102 | } |
| 4125 | 4103 | |
| ... | ... | @@ -4127,7 +4105,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4127 | 4105 | const gpa = self.base.comp.gpa; |
| 4128 | 4106 | const slice = self.sections.slice(); |
| 4129 | 4107 | |
| 4130 | | if (self.interp_section_index) |shndx| { |
| 4108 | if (self.section_indexes.interp) |shndx| { |
| 4131 | 4109 | var buffer: [256]u8 = undefined; |
| 4132 | 4110 | const interp = self.getTarget().dynamic_linker.get().?; |
| 4133 | 4111 | @memcpy(buffer[0..interp.len], interp); |
| ... | ... | @@ -4138,12 +4116,12 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4138 | 4116 | try self.base.file.?.pwriteAll(contents, shdr.sh_offset); |
| 4139 | 4117 | } |
| 4140 | 4118 | |
| 4141 | | if (self.hash_section_index) |shndx| { |
| 4119 | if (self.section_indexes.hash) |shndx| { |
| 4142 | 4120 | const shdr = slice.items(.shdr)[shndx]; |
| 4143 | 4121 | try self.base.file.?.pwriteAll(self.hash.buffer.items, shdr.sh_offset); |
| 4144 | 4122 | } |
| 4145 | 4123 | |
| 4146 | | if (self.gnu_hash_section_index) |shndx| { |
| 4124 | if (self.section_indexes.gnu_hash) |shndx| { |
| 4147 | 4125 | const shdr = slice.items(.shdr)[shndx]; |
| 4148 | 4126 | var buffer = try std.ArrayList(u8).initCapacity(gpa, self.gnu_hash.size()); |
| 4149 | 4127 | defer buffer.deinit(); |
| ... | ... | @@ -4151,12 +4129,12 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4151 | 4129 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); |
| 4152 | 4130 | } |
| 4153 | 4131 | |
| 4154 | | if (self.versym_section_index) |shndx| { |
| 4132 | if (self.section_indexes.versym) |shndx| { |
| 4155 | 4133 | const shdr = slice.items(.shdr)[shndx]; |
| 4156 | 4134 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.versym.items), shdr.sh_offset); |
| 4157 | 4135 | } |
| 4158 | 4136 | |
| 4159 | | if (self.verneed_section_index) |shndx| { |
| 4137 | if (self.section_indexes.verneed) |shndx| { |
| 4160 | 4138 | const shdr = slice.items(.shdr)[shndx]; |
| 4161 | 4139 | var buffer = try std.ArrayList(u8).initCapacity(gpa, self.verneed.size()); |
| 4162 | 4140 | defer buffer.deinit(); |
| ... | ... | @@ -4164,7 +4142,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4164 | 4142 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); |
| 4165 | 4143 | } |
| 4166 | 4144 | |
| 4167 | | if (self.dynamic_section_index) |shndx| { |
| 4145 | if (self.section_indexes.dynamic) |shndx| { |
| 4168 | 4146 | const shdr = slice.items(.shdr)[shndx]; |
| 4169 | 4147 | var buffer = try std.ArrayList(u8).initCapacity(gpa, self.dynamic.size(self)); |
| 4170 | 4148 | defer buffer.deinit(); |
| ... | ... | @@ -4172,7 +4150,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4172 | 4150 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); |
| 4173 | 4151 | } |
| 4174 | 4152 | |
| 4175 | | if (self.dynsymtab_section_index) |shndx| { |
| 4153 | if (self.section_indexes.dynsymtab) |shndx| { |
| 4176 | 4154 | const shdr = slice.items(.shdr)[shndx]; |
| 4177 | 4155 | var buffer = try std.ArrayList(u8).initCapacity(gpa, self.dynsym.size()); |
| 4178 | 4156 | defer buffer.deinit(); |
| ... | ... | @@ -4180,12 +4158,12 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4180 | 4158 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); |
| 4181 | 4159 | } |
| 4182 | 4160 | |
| 4183 | | if (self.dynstrtab_section_index) |shndx| { |
| 4161 | if (self.section_indexes.dynstrtab) |shndx| { |
| 4184 | 4162 | const shdr = slice.items(.shdr)[shndx]; |
| 4185 | 4163 | try self.base.file.?.pwriteAll(self.dynstrtab.items, shdr.sh_offset); |
| 4186 | 4164 | } |
| 4187 | 4165 | |
| 4188 | | if (self.eh_frame_section_index) |shndx| { |
| 4166 | if (self.section_indexes.eh_frame) |shndx| { |
| 4189 | 4167 | const existing_size = existing_size: { |
| 4190 | 4168 | const zo = self.zigObjectPtr() orelse break :existing_size 0; |
| 4191 | 4169 | const sym = zo.symbol(zo.eh_frame_index orelse break :existing_size 0); |
| ... | ... | @@ -4200,7 +4178,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4200 | 4178 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset + existing_size); |
| 4201 | 4179 | } |
| 4202 | 4180 | |
| 4203 | | if (self.eh_frame_hdr_section_index) |shndx| { |
| 4181 | if (self.section_indexes.eh_frame_hdr) |shndx| { |
| 4204 | 4182 | const shdr = slice.items(.shdr)[shndx]; |
| 4205 | 4183 | const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow; |
| 4206 | 4184 | var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size); |
| ... | ... | @@ -4209,7 +4187,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4209 | 4187 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); |
| 4210 | 4188 | } |
| 4211 | 4189 | |
| 4212 | | if (self.got_section_index) |index| { |
| 4190 | if (self.section_indexes.got) |index| { |
| 4213 | 4191 | const shdr = slice.items(.shdr)[index]; |
| 4214 | 4192 | var buffer = try std.ArrayList(u8).initCapacity(gpa, self.got.size(self)); |
| 4215 | 4193 | defer buffer.deinit(); |
| ... | ... | @@ -4217,7 +4195,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4217 | 4195 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); |
| 4218 | 4196 | } |
| 4219 | 4197 | |
| 4220 | | if (self.rela_dyn_section_index) |shndx| { |
| 4198 | if (self.section_indexes.rela_dyn) |shndx| { |
| 4221 | 4199 | const shdr = slice.items(.shdr)[shndx]; |
| 4222 | 4200 | try self.got.addRela(self); |
| 4223 | 4201 | try self.copy_rel.addRela(self); |
| ... | ... | @@ -4225,7 +4203,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4225 | 4203 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_dyn.items), shdr.sh_offset); |
| 4226 | 4204 | } |
| 4227 | 4205 | |
| 4228 | | if (self.plt_section_index) |shndx| { |
| 4206 | if (self.section_indexes.plt) |shndx| { |
| 4229 | 4207 | const shdr = slice.items(.shdr)[shndx]; |
| 4230 | 4208 | var buffer = try std.ArrayList(u8).initCapacity(gpa, self.plt.size(self)); |
| 4231 | 4209 | defer buffer.deinit(); |
| ... | ... | @@ -4233,7 +4211,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4233 | 4211 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); |
| 4234 | 4212 | } |
| 4235 | 4213 | |
| 4236 | | if (self.got_plt_section_index) |shndx| { |
| 4214 | if (self.section_indexes.got_plt) |shndx| { |
| 4237 | 4215 | const shdr = slice.items(.shdr)[shndx]; |
| 4238 | 4216 | var buffer = try std.ArrayList(u8).initCapacity(gpa, self.got_plt.size(self)); |
| 4239 | 4217 | defer buffer.deinit(); |
| ... | ... | @@ -4241,7 +4219,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4241 | 4219 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); |
| 4242 | 4220 | } |
| 4243 | 4221 | |
| 4244 | | if (self.plt_got_section_index) |shndx| { |
| 4222 | if (self.section_indexes.plt_got) |shndx| { |
| 4245 | 4223 | const shdr = slice.items(.shdr)[shndx]; |
| 4246 | 4224 | var buffer = try std.ArrayList(u8).initCapacity(gpa, self.plt_got.size(self)); |
| 4247 | 4225 | defer buffer.deinit(); |
| ... | ... | @@ -4249,7 +4227,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4249 | 4227 | try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset); |
| 4250 | 4228 | } |
| 4251 | 4229 | |
| 4252 | | if (self.rela_plt_section_index) |shndx| { |
| 4230 | if (self.section_indexes.rela_plt) |shndx| { |
| 4253 | 4231 | const shdr = slice.items(.shdr)[shndx]; |
| 4254 | 4232 | try self.plt.addRela(self); |
| 4255 | 4233 | try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_plt.items), shdr.sh_offset); |
| ... | ... | @@ -4261,7 +4239,7 @@ fn writeSyntheticSections(self: *Elf) !void { |
| 4261 | 4239 | |
| 4262 | 4240 | // FIXME:JK again, why is this needed? |
| 4263 | 4241 | pub fn writeShStrtab(self: *Elf) !void { |
| 4264 | | if (self.shstrtab_section_index) |index| { |
| 4242 | if (self.section_indexes.shstrtab) |index| { |
| 4265 | 4243 | const shdr = self.sections.items(.shdr)[index]; |
| 4266 | 4244 | log.debug("writing .shstrtab from 0x{x} to 0x{x}", .{ shdr.sh_offset, shdr.sh_offset + shdr.sh_size }); |
| 4267 | 4245 | try self.base.file.?.pwriteAll(self.shstrtab.items, shdr.sh_offset); |
| ... | ... | @@ -4271,8 +4249,8 @@ pub fn writeShStrtab(self: *Elf) !void { |
| 4271 | 4249 | pub fn writeSymtab(self: *Elf) !void { |
| 4272 | 4250 | const gpa = self.base.comp.gpa; |
| 4273 | 4251 | const slice = self.sections.slice(); |
| 4274 | | const symtab_shdr = slice.items(.shdr)[self.symtab_section_index.?]; |
| 4275 | | const strtab_shdr = slice.items(.shdr)[self.strtab_section_index.?]; |
| 4252 | const symtab_shdr = slice.items(.shdr)[self.section_indexes.symtab.?]; |
| 4253 | const strtab_shdr = slice.items(.shdr)[self.section_indexes.strtab.?]; |
| 4276 | 4254 | const sym_size: u64 = switch (self.ptr_width) { |
| 4277 | 4255 | .p32 => @sizeOf(elf.Elf32_Sym), |
| 4278 | 4256 | .p64 => @sizeOf(elf.Elf64_Sym), |
| ... | ... | @@ -4330,15 +4308,15 @@ pub fn writeSymtab(self: *Elf) !void { |
| 4330 | 4308 | obj.asFile().writeSymtab(self); |
| 4331 | 4309 | } |
| 4332 | 4310 | |
| 4333 | | if (self.got_section_index) |_| { |
| 4311 | if (self.section_indexes.got) |_| { |
| 4334 | 4312 | self.got.writeSymtab(self); |
| 4335 | 4313 | } |
| 4336 | 4314 | |
| 4337 | | if (self.plt_section_index) |_| { |
| 4315 | if (self.section_indexes.plt) |_| { |
| 4338 | 4316 | self.plt.writeSymtab(self); |
| 4339 | 4317 | } |
| 4340 | 4318 | |
| 4341 | | if (self.plt_got_section_index) |_| { |
| 4319 | if (self.section_indexes.plt_got) |_| { |
| 4342 | 4320 | self.plt_got.writeSymtab(self); |
| 4343 | 4321 | } |
| 4344 | 4322 | |
| ... | ... | @@ -4778,9 +4756,9 @@ pub fn mergeSection(self: *Elf, index: MergeSection.Index) *MergeSection { |
| 4778 | 4756 | |
| 4779 | 4757 | pub fn gotAddress(self: *Elf) i64 { |
| 4780 | 4758 | const shndx = blk: { |
| 4781 | | if (self.getTarget().cpu.arch == .x86_64 and self.got_plt_section_index != null) |
| 4782 | | break :blk self.got_plt_section_index.?; |
| 4783 | | break :blk if (self.got_section_index) |shndx| shndx else null; |
| 4759 | if (self.getTarget().cpu.arch == .x86_64 and self.section_indexes.got_plt != null) |
| 4760 | break :blk self.section_indexes.got_plt.?; |
| 4761 | break :blk if (self.section_indexes.got) |shndx| shndx else null; |
| 4784 | 4762 | }; |
| 4785 | 4763 | return if (shndx) |index| @intCast(self.sections.items(.shdr)[index].sh_addr) else 0; |
| 4786 | 4764 | } |