authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-10 17:35:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-11 10:33:54-07:00
log2c4e05eda7c3b6e27e83f85ca0a7c501c15775e7
treead351533e5121dff472437c6af922e8c7edada53
parentc3148988a3e1ccbe351b8d51359490eb8258c18f

link.Elf: group section indexes

so they cannot be forgotten when updating them after sorting them.

8 files changed, 229 insertions(+), 251 deletions(-)

src/link/Elf.zig+167-189
...@@ -57,6 +57,7 @@ phdrs: ProgramHeaderList = .empty,...@@ -57,6 +57,7 @@ phdrs: ProgramHeaderList = .empty,
5757
58/// Special program headers.58/// Special program headers.
59phdr_indexes: ProgramHeaderIndexes = .{},59phdr_indexes: ProgramHeaderIndexes = .{},
60section_indexes: SectionIndexes = .{},
6061
61page_size: u32,62page_size: u32,
62default_sym_version: elf.Elf64_Versym,63default_sym_version: elf.Elf64_Versym,
...@@ -99,29 +100,6 @@ rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .empty,...@@ -99,29 +100,6 @@ rela_plt: std.ArrayListUnmanaged(elf.Elf64_Rela) = .empty,
99/// Applies only to a relocatable.100/// Applies only to a relocatable.
100comdat_group_sections: std.ArrayListUnmanaged(ComdatGroupSection) = .empty,101comdat_group_sections: std.ArrayListUnmanaged(ComdatGroupSection) = .empty,
101102
102copy_rel_section_index: ?u32 = null,
103dynamic_section_index: ?u32 = null,
104dynstrtab_section_index: ?u32 = null,
105dynsymtab_section_index: ?u32 = null,
106eh_frame_section_index: ?u32 = null,
107eh_frame_rela_section_index: ?u32 = null,
108eh_frame_hdr_section_index: ?u32 = null,
109hash_section_index: ?u32 = null,
110gnu_hash_section_index: ?u32 = null,
111got_section_index: ?u32 = null,
112got_plt_section_index: ?u32 = null,
113interp_section_index: ?u32 = null,
114plt_section_index: ?u32 = null,
115plt_got_section_index: ?u32 = null,
116rela_dyn_section_index: ?u32 = null,
117rela_plt_section_index: ?u32 = null,
118versym_section_index: ?u32 = null,
119verneed_section_index: ?u32 = null,
120
121shstrtab_section_index: ?u32 = null,
122strtab_section_index: ?u32 = null,
123symtab_section_index: ?u32 = null,
124
125resolver: SymbolResolver = .{},103resolver: SymbolResolver = .{},
126104
127has_text_reloc: bool = false,105has_text_reloc: bool = false,
...@@ -136,6 +114,31 @@ comment_merge_section_index: ?MergeSection.Index = null,...@@ -136,6 +114,31 @@ comment_merge_section_index: ?MergeSection.Index = null,
136114
137first_eflags: ?elf.Elf64_Word = null,115first_eflags: ?elf.Elf64_Word = null,
138116
117const 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
139const ProgramHeaderList = std.ArrayListUnmanaged(elf.Elf64_Phdr);142const ProgramHeaderList = std.ArrayListUnmanaged(elf.Elf64_Phdr);
140143
141const OptionalProgramHeaderIndex = enum(u16) {144const OptionalProgramHeaderIndex = enum(u16) {
...@@ -2670,7 +2673,7 @@ pub fn writeElfHeader(self: *Elf) !void {...@@ -2670,7 +2673,7 @@ pub fn writeElfHeader(self: *Elf) !void {
2670 mem.writeInt(u16, hdr_buf[index..][0..2], e_shnum, endian);2673 mem.writeInt(u16, hdr_buf[index..][0..2], e_shnum, endian);
2671 index += 2;2674 index += 2;
26722675
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 index += 2;2677 index += 2;
26752678
2676 assert(index == e_ehsize);2679 assert(index == e_ehsize);
...@@ -2888,8 +2891,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -2888,8 +2891,8 @@ fn initSyntheticSections(self: *Elf) !void {
2888 } else false;2891 } else false;
2889 };2892 };
2890 if (needs_eh_frame) {2893 if (needs_eh_frame) {
2891 if (self.eh_frame_section_index == null) {2894 if (self.section_indexes.eh_frame == null) {
2892 self.eh_frame_section_index = self.sectionByName(".eh_frame") orelse try self.addSection(.{2895 self.section_indexes.eh_frame = self.sectionByName(".eh_frame") orelse try self.addSection(.{
2893 .name = try self.insertShString(".eh_frame"),2896 .name = try self.insertShString(".eh_frame"),
2894 .type = if (target.cpu.arch == .x86_64)2897 .type = if (target.cpu.arch == .x86_64)
2895 elf.SHT_X86_64_UNWIND2898 elf.SHT_X86_64_UNWIND
...@@ -2899,8 +2902,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -2899,8 +2902,8 @@ fn initSyntheticSections(self: *Elf) !void {
2899 .addralign = ptr_size,2902 .addralign = ptr_size,
2900 });2903 });
2901 }2904 }
2902 if (comp.link_eh_frame_hdr and self.eh_frame_hdr_section_index == null) {2905 if (comp.link_eh_frame_hdr and self.section_indexes.eh_frame_hdr == null) {
2903 self.eh_frame_hdr_section_index = try self.addSection(.{2906 self.section_indexes.eh_frame_hdr = try self.addSection(.{
2904 .name = try self.insertShString(".eh_frame_hdr"),2907 .name = try self.insertShString(".eh_frame_hdr"),
2905 .type = elf.SHT_PROGBITS,2908 .type = elf.SHT_PROGBITS,
2906 .flags = elf.SHF_ALLOC,2909 .flags = elf.SHF_ALLOC,
...@@ -2909,8 +2912,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -2909,8 +2912,8 @@ fn initSyntheticSections(self: *Elf) !void {
2909 }2912 }
2910 }2913 }
29112914
2912 if (self.got.entries.items.len > 0 and self.got_section_index == null) {2915 if (self.got.entries.items.len > 0 and self.section_indexes.got == null) {
2913 self.got_section_index = try self.addSection(.{2916 self.section_indexes.got = try self.addSection(.{
2914 .name = try self.insertShString(".got"),2917 .name = try self.insertShString(".got"),
2915 .type = elf.SHT_PROGBITS,2918 .type = elf.SHT_PROGBITS,
2916 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,2919 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
...@@ -2918,8 +2921,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -2918,8 +2921,8 @@ fn initSyntheticSections(self: *Elf) !void {
2918 });2921 });
2919 }2922 }
29202923
2921 if (self.got_plt_section_index == null) {2924 if (self.section_indexes.got_plt == null) {
2922 self.got_plt_section_index = try self.addSection(.{2925 self.section_indexes.got_plt = try self.addSection(.{
2923 .name = try self.insertShString(".got.plt"),2926 .name = try self.insertShString(".got.plt"),
2924 .type = elf.SHT_PROGBITS,2927 .type = elf.SHT_PROGBITS,
2925 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,2928 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
...@@ -2938,8 +2941,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -2938,8 +2941,8 @@ fn initSyntheticSections(self: *Elf) !void {
2938 }2941 }
2939 break :blk false;2942 break :blk false;
2940 };2943 };
2941 if (needs_rela_dyn and self.rela_dyn_section_index == null) {2944 if (needs_rela_dyn and self.section_indexes.rela_dyn == null) {
2942 self.rela_dyn_section_index = try self.addSection(.{2945 self.section_indexes.rela_dyn = try self.addSection(.{
2943 .name = try self.insertShString(".rela.dyn"),2946 .name = try self.insertShString(".rela.dyn"),
2944 .type = elf.SHT_RELA,2947 .type = elf.SHT_RELA,
2945 .flags = elf.SHF_ALLOC,2948 .flags = elf.SHF_ALLOC,
...@@ -2949,16 +2952,16 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -2949,16 +2952,16 @@ fn initSyntheticSections(self: *Elf) !void {
2949 }2952 }
29502953
2951 if (self.plt.symbols.items.len > 0) {2954 if (self.plt.symbols.items.len > 0) {
2952 if (self.plt_section_index == null) {2955 if (self.section_indexes.plt == null) {
2953 self.plt_section_index = try self.addSection(.{2956 self.section_indexes.plt = try self.addSection(.{
2954 .name = try self.insertShString(".plt"),2957 .name = try self.insertShString(".plt"),
2955 .type = elf.SHT_PROGBITS,2958 .type = elf.SHT_PROGBITS,
2956 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,2959 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
2957 .addralign = 16,2960 .addralign = 16,
2958 });2961 });
2959 }2962 }
2960 if (self.rela_plt_section_index == null) {2963 if (self.section_indexes.rela_plt == null) {
2961 self.rela_plt_section_index = try self.addSection(.{2964 self.section_indexes.rela_plt = try self.addSection(.{
2962 .name = try self.insertShString(".rela.plt"),2965 .name = try self.insertShString(".rela.plt"),
2963 .type = elf.SHT_RELA,2966 .type = elf.SHT_RELA,
2964 .flags = elf.SHF_ALLOC,2967 .flags = elf.SHF_ALLOC,
...@@ -2968,8 +2971,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -2968,8 +2971,8 @@ fn initSyntheticSections(self: *Elf) !void {
2968 }2971 }
2969 }2972 }
29702973
2971 if (self.plt_got.symbols.items.len > 0 and self.plt_got_section_index == null) {2974 if (self.plt_got.symbols.items.len > 0 and self.section_indexes.plt_got == null) {
2972 self.plt_got_section_index = try self.addSection(.{2975 self.section_indexes.plt_got = try self.addSection(.{
2973 .name = try self.insertShString(".plt.got"),2976 .name = try self.insertShString(".plt.got"),
2974 .type = elf.SHT_PROGBITS,2977 .type = elf.SHT_PROGBITS,
2975 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,2978 .flags = elf.SHF_ALLOC | elf.SHF_EXECINSTR,
...@@ -2977,8 +2980,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -2977,8 +2980,8 @@ fn initSyntheticSections(self: *Elf) !void {
2977 });2980 });
2978 }2981 }
29792982
2980 if (self.copy_rel.symbols.items.len > 0 and self.copy_rel_section_index == null) {2983 if (self.copy_rel.symbols.items.len > 0 and self.section_indexes.copy_rel == null) {
2981 self.copy_rel_section_index = try self.addSection(.{2984 self.section_indexes.copy_rel = try self.addSection(.{
2982 .name = try self.insertShString(".copyrel"),2985 .name = try self.insertShString(".copyrel"),
2983 .type = elf.SHT_NOBITS,2986 .type = elf.SHT_NOBITS,
2984 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,2987 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
...@@ -2994,8 +2997,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -2994,8 +2997,8 @@ fn initSyntheticSections(self: *Elf) !void {
2994 if (self.base.isStatic() and !comp.config.pie) break :blk false;2997 if (self.base.isStatic() and !comp.config.pie) break :blk false;
2995 break :blk target.dynamic_linker.get() != null;2998 break :blk target.dynamic_linker.get() != null;
2996 };2999 };
2997 if (needs_interp and self.interp_section_index == null) {3000 if (needs_interp and self.section_indexes.interp == null) {
2998 self.interp_section_index = try self.addSection(.{3001 self.section_indexes.interp = try self.addSection(.{
2999 .name = try self.insertShString(".interp"),3002 .name = try self.insertShString(".interp"),
3000 .type = elf.SHT_PROGBITS,3003 .type = elf.SHT_PROGBITS,
3001 .flags = elf.SHF_ALLOC,3004 .flags = elf.SHF_ALLOC,
...@@ -3004,8 +3007,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3004,8 +3007,8 @@ fn initSyntheticSections(self: *Elf) !void {
3004 }3007 }
30053008
3006 if (self.isEffectivelyDynLib() or self.shared_objects.items.len > 0 or comp.config.pie) {3009 if (self.isEffectivelyDynLib() or self.shared_objects.items.len > 0 or comp.config.pie) {
3007 if (self.dynstrtab_section_index == null) {3010 if (self.section_indexes.dynstrtab == null) {
3008 self.dynstrtab_section_index = try self.addSection(.{3011 self.section_indexes.dynstrtab = try self.addSection(.{
3009 .name = try self.insertShString(".dynstr"),3012 .name = try self.insertShString(".dynstr"),
3010 .flags = elf.SHF_ALLOC,3013 .flags = elf.SHF_ALLOC,
3011 .type = elf.SHT_STRTAB,3014 .type = elf.SHT_STRTAB,
...@@ -3013,8 +3016,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3013,8 +3016,8 @@ fn initSyntheticSections(self: *Elf) !void {
3013 .addralign = 1,3016 .addralign = 1,
3014 });3017 });
3015 }3018 }
3016 if (self.dynamic_section_index == null) {3019 if (self.section_indexes.dynamic == null) {
3017 self.dynamic_section_index = try self.addSection(.{3020 self.section_indexes.dynamic = try self.addSection(.{
3018 .name = try self.insertShString(".dynamic"),3021 .name = try self.insertShString(".dynamic"),
3019 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,3022 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
3020 .type = elf.SHT_DYNAMIC,3023 .type = elf.SHT_DYNAMIC,
...@@ -3022,8 +3025,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3022,8 +3025,8 @@ fn initSyntheticSections(self: *Elf) !void {
3022 .addralign = @alignOf(elf.Elf64_Dyn),3025 .addralign = @alignOf(elf.Elf64_Dyn),
3023 });3026 });
3024 }3027 }
3025 if (self.dynsymtab_section_index == null) {3028 if (self.section_indexes.dynsymtab == null) {
3026 self.dynsymtab_section_index = try self.addSection(.{3029 self.section_indexes.dynsymtab = try self.addSection(.{
3027 .name = try self.insertShString(".dynsym"),3030 .name = try self.insertShString(".dynsym"),
3028 .flags = elf.SHF_ALLOC,3031 .flags = elf.SHF_ALLOC,
3029 .type = elf.SHT_DYNSYM,3032 .type = elf.SHT_DYNSYM,
...@@ -3032,8 +3035,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3032,8 +3035,8 @@ fn initSyntheticSections(self: *Elf) !void {
3032 .info = 1,3035 .info = 1,
3033 });3036 });
3034 }3037 }
3035 if (self.hash_section_index == null) {3038 if (self.section_indexes.hash == null) {
3036 self.hash_section_index = try self.addSection(.{3039 self.section_indexes.hash = try self.addSection(.{
3037 .name = try self.insertShString(".hash"),3040 .name = try self.insertShString(".hash"),
3038 .flags = elf.SHF_ALLOC,3041 .flags = elf.SHF_ALLOC,
3039 .type = elf.SHT_HASH,3042 .type = elf.SHT_HASH,
...@@ -3041,8 +3044,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3041,8 +3044,8 @@ fn initSyntheticSections(self: *Elf) !void {
3041 .entsize = 4,3044 .entsize = 4,
3042 });3045 });
3043 }3046 }
3044 if (self.gnu_hash_section_index == null) {3047 if (self.section_indexes.gnu_hash == null) {
3045 self.gnu_hash_section_index = try self.addSection(.{3048 self.section_indexes.gnu_hash = try self.addSection(.{
3046 .name = try self.insertShString(".gnu.hash"),3049 .name = try self.insertShString(".gnu.hash"),
3047 .flags = elf.SHF_ALLOC,3050 .flags = elf.SHF_ALLOC,
3048 .type = elf.SHT_GNU_HASH,3051 .type = elf.SHT_GNU_HASH,
...@@ -3055,8 +3058,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3055,8 +3058,8 @@ fn initSyntheticSections(self: *Elf) !void {
3055 if (sym.flags.import and sym.version_index & elf.VERSYM_VERSION > elf.VER_NDX_GLOBAL) break true;3058 if (sym.flags.import and sym.version_index & elf.VERSYM_VERSION > elf.VER_NDX_GLOBAL) break true;
3056 } else false;3059 } else false;
3057 if (needs_versions) {3060 if (needs_versions) {
3058 if (self.versym_section_index == null) {3061 if (self.section_indexes.versym == null) {
3059 self.versym_section_index = try self.addSection(.{3062 self.section_indexes.versym = try self.addSection(.{
3060 .name = try self.insertShString(".gnu.version"),3063 .name = try self.insertShString(".gnu.version"),
3061 .flags = elf.SHF_ALLOC,3064 .flags = elf.SHF_ALLOC,
3062 .type = elf.SHT_GNU_VERSYM,3065 .type = elf.SHT_GNU_VERSYM,
...@@ -3064,8 +3067,8 @@ fn initSyntheticSections(self: *Elf) !void {...@@ -3064,8 +3067,8 @@ fn initSyntheticSections(self: *Elf) !void {
3064 .entsize = @sizeOf(elf.Elf64_Versym),3067 .entsize = @sizeOf(elf.Elf64_Versym),
3065 });3068 });
3066 }3069 }
3067 if (self.verneed_section_index == null) {3070 if (self.section_indexes.verneed == null) {
3068 self.verneed_section_index = try self.addSection(.{3071 self.section_indexes.verneed = try self.addSection(.{
3069 .name = try self.insertShString(".gnu.version_r"),3072 .name = try self.insertShString(".gnu.version_r"),
3070 .flags = elf.SHF_ALLOC,3073 .flags = elf.SHF_ALLOC,
3071 .type = elf.SHT_GNU_VERNEED,3074 .type = elf.SHT_GNU_VERNEED,
...@@ -3084,16 +3087,16 @@ pub fn initSymtab(self: *Elf) !void {...@@ -3084,16 +3087,16 @@ pub fn initSymtab(self: *Elf) !void {
3084 .p32 => true,3087 .p32 => true,
3085 .p64 => false,3088 .p64 => false,
3086 };3089 };
3087 if (self.symtab_section_index == null) {3090 if (self.section_indexes.symtab == null) {
3088 self.symtab_section_index = try self.addSection(.{3091 self.section_indexes.symtab = try self.addSection(.{
3089 .name = try self.insertShString(".symtab"),3092 .name = try self.insertShString(".symtab"),
3090 .type = elf.SHT_SYMTAB,3093 .type = elf.SHT_SYMTAB,
3091 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),3094 .addralign = if (small_ptr) @alignOf(elf.Elf32_Sym) else @alignOf(elf.Elf64_Sym),
3092 .entsize = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym),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) {3098 if (self.section_indexes.strtab == null) {
3096 self.strtab_section_index = try self.addSection(.{3099 self.section_indexes.strtab = try self.addSection(.{
3097 .name = try self.insertShString(".strtab"),3100 .name = try self.insertShString(".strtab"),
3098 .type = elf.SHT_STRTAB,3101 .type = elf.SHT_STRTAB,
3099 .entsize = 1,3102 .entsize = 1,
...@@ -3103,8 +3106,8 @@ pub fn initSymtab(self: *Elf) !void {...@@ -3103,8 +3106,8 @@ pub fn initSymtab(self: *Elf) !void {
3103}3106}
31043107
3105pub fn initShStrtab(self: *Elf) !void {3108pub fn initShStrtab(self: *Elf) !void {
3106 if (self.shstrtab_section_index == null) {3109 if (self.section_indexes.shstrtab == null) {
3107 self.shstrtab_section_index = try self.addSection(.{3110 self.section_indexes.shstrtab = try self.addSection(.{
3108 .name = try self.insertShString(".shstrtab"),3111 .name = try self.insertShString(".shstrtab"),
3109 .type = elf.SHT_STRTAB,3112 .type = elf.SHT_STRTAB,
3110 .entsize = 1,3113 .entsize = 1,
...@@ -3116,20 +3119,20 @@ pub fn initShStrtab(self: *Elf) !void {...@@ -3116,20 +3119,20 @@ pub fn initShStrtab(self: *Elf) !void {
3116fn initSpecialPhdrs(self: *Elf) !void {3119fn initSpecialPhdrs(self: *Elf) !void {
3117 comptime assert(max_number_of_special_phdrs == 5);3120 comptime assert(max_number_of_special_phdrs == 5);
31183121
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 self.phdr_indexes.interp = (try self.addPhdr(.{3123 self.phdr_indexes.interp = (try self.addPhdr(.{
3121 .type = elf.PT_INTERP,3124 .type = elf.PT_INTERP,
3122 .flags = elf.PF_R,3125 .flags = elf.PF_R,
3123 .@"align" = 1,3126 .@"align" = 1,
3124 })).toOptional();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 self.phdr_indexes.dynamic = (try self.addPhdr(.{3130 self.phdr_indexes.dynamic = (try self.addPhdr(.{
3128 .type = elf.PT_DYNAMIC,3131 .type = elf.PT_DYNAMIC,
3129 .flags = elf.PF_R | elf.PF_W,3132 .flags = elf.PF_R | elf.PF_W,
3130 })).toOptional();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 self.phdr_indexes.gnu_eh_frame = (try self.addPhdr(.{3136 self.phdr_indexes.gnu_eh_frame = (try self.addPhdr(.{
3134 .type = elf.PT_GNU_EH_FRAME,3137 .type = elf.PT_GNU_EH_FRAME,
3135 .flags = elf.PF_R,3138 .flags = elf.PF_R,
...@@ -3237,7 +3240,7 @@ fn sortInitFini(self: *Elf) !void {...@@ -3237,7 +3240,7 @@ fn sortInitFini(self: *Elf) !void {
3237}3240}
32383241
3239fn setDynamicSection(self: *Elf, rpaths: []const []const u8) !void {3242fn setDynamicSection(self: *Elf, rpaths: []const []const u8) !void {
3240 if (self.dynamic_section_index == null) return;3243 if (self.section_indexes.dynamic == null) return;
32413244
3242 for (self.shared_objects.items) |index| {3245 for (self.shared_objects.items) |index| {
3243 const shared_object = self.file(index).?.shared_object;3246 const shared_object = self.file(index).?.shared_object;
...@@ -3255,13 +3258,13 @@ fn setDynamicSection(self: *Elf, rpaths: []const []const u8) !void {...@@ -3255,13 +3258,13 @@ fn setDynamicSection(self: *Elf, rpaths: []const []const u8) !void {
3255}3258}
32563259
3257fn sortDynamicSymtab(self: *Elf) void {3260fn sortDynamicSymtab(self: *Elf) void {
3258 if (self.gnu_hash_section_index == null) return;3261 if (self.section_indexes.gnu_hash == null) return;
3259 self.dynsym.sort(self);3262 self.dynsym.sort(self);
3260}3263}
32613264
3262fn setVersionSymtab(self: *Elf) !void {3265fn setVersionSymtab(self: *Elf) !void {
3263 const gpa = self.base.comp.gpa;3266 const gpa = self.base.comp.gpa;
3264 if (self.versym_section_index == null) return;3267 if (self.section_indexes.versym == null) return;
3265 try self.versym.resize(gpa, self.dynsym.count());3268 try self.versym.resize(gpa, self.dynsym.count());
3266 self.versym.items[0] = elf.VER_NDX_LOCAL;3269 self.versym.items[0] = elf.VER_NDX_LOCAL;
3267 for (self.dynsym.entries.items, 1..) |entry, i| {3270 for (self.dynsym.entries.items, 1..) |entry, i| {
...@@ -3269,7 +3272,7 @@ fn setVersionSymtab(self: *Elf) !void {...@@ -3269,7 +3272,7 @@ fn setVersionSymtab(self: *Elf) !void {
3269 self.versym.items[i] = sym.version_index;3272 self.versym.items[i] = sym.version_index;
3270 }3273 }
32713274
3272 if (self.verneed_section_index) |shndx| {3275 if (self.section_indexes.verneed) |shndx| {
3273 try self.verneed.generate(self);3276 try self.verneed.generate(self);
3274 const shdr = &self.sections.items(.shdr)[shndx];3277 const shdr = &self.sections.items(.shdr)[shndx];
3275 shdr.sh_info = @as(u32, @intCast(self.verneed.verneed.items.len));3278 shdr.sh_info = @as(u32, @intCast(self.verneed.verneed.items.len));
...@@ -3277,10 +3280,10 @@ fn setVersionSymtab(self: *Elf) !void {...@@ -3277,10 +3280,10 @@ fn setVersionSymtab(self: *Elf) !void {
3277}3280}
32783281
3279fn setHashSections(self: *Elf) !void {3282fn setHashSections(self: *Elf) !void {
3280 if (self.hash_section_index != null) {3283 if (self.section_indexes.hash != null) {
3281 try self.hash.generate(self);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 try self.gnu_hash.calcSize(self);3287 try self.gnu_hash.calcSize(self);
3285 }3288 }
3286}3289}
...@@ -3420,47 +3423,22 @@ pub fn sortShdrs(self: *Elf) !void {...@@ -3420,47 +3423,22 @@ pub fn sortShdrs(self: *Elf) !void {
34203423
3421 const backlinks = try gpa.alloc(u32, entries.items.len);3424 const backlinks = try gpa.alloc(u32, entries.items.len);
3422 defer gpa.free(backlinks);3425 defer gpa.free(backlinks);
3423 for (entries.items, 0..) |entry, i| {3426 {
3424 backlinks[entry.shndx] = @intCast(i);3427 var slice = self.sections.toOwnedSlice();
3425 }3428 defer slice.deinit(gpa);
34263429 try self.sections.resize(gpa, slice.len);
3427 var slice = self.sections.toOwnedSlice();3430
3428 defer slice.deinit(gpa);3431 for (entries.items, 0..) |entry, i| {
34293432 backlinks[entry.shndx] = @intCast(i);
3430 try self.sections.ensureTotalCapacity(gpa, slice.len);3433 self.sections.set(i, slice.get(entry.shndx));
3431 for (entries.items) |sorted| {3434 }
3432 self.sections.appendAssumeCapacity(slice.get(sorted.shndx));3435 }
3433 }3436
34343437 const special_indexes = &self.section_indexes;
3435 self.resetShdrIndexes(backlinks);3438
3436}3439 inline for (@typeInfo(SectionIndexes).@"struct".fields) |field| {
34373440 if (@field(special_indexes, field.name)) |special_index| {
3438fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {3441 @field(special_indexes, field.name) = backlinks[special_index];
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.*];
3464 }3442 }
3465 }3443 }
34663444
...@@ -3478,7 +3456,7 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {...@@ -3478,7 +3456,7 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
3478 // FIXME:JK we should spin up .symtab potentially earlier, or set all non-dynamic RELA sections3456 // FIXME:JK we should spin up .symtab potentially earlier, or set all non-dynamic RELA sections
3479 // to point at symtab3457 // to point at symtab
3480 // shdr.sh_link = backlinks[shdr.sh_link];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 shdr.sh_info = backlinks[shdr.sh_info];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,56 +3467,56 @@ fn resetShdrIndexes(self: *Elf, backlinks: []const u32) void {
3489 cg.shndx = backlinks[cg.shndx];3467 cg.shndx = backlinks[cg.shndx];
3490 }3468 }
34913469
3492 if (self.symtab_section_index) |index| {3470 if (self.section_indexes.symtab) |index| {
3493 const shdr = &slice.items(.shdr)[index];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 }
34963474
3497 if (self.dynamic_section_index) |index| {3475 if (self.section_indexes.dynamic) |index| {
3498 const shdr = &slice.items(.shdr)[index];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 }
35013479
3502 if (self.dynsymtab_section_index) |index| {3480 if (self.section_indexes.dynsymtab) |index| {
3503 const shdr = &slice.items(.shdr)[index];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 }
35063484
3507 if (self.hash_section_index) |index| {3485 if (self.section_indexes.hash) |index| {
3508 const shdr = &slice.items(.shdr)[index];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 }
35113489
3512 if (self.gnu_hash_section_index) |index| {3490 if (self.section_indexes.gnu_hash) |index| {
3513 const shdr = &slice.items(.shdr)[index];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 }
35163494
3517 if (self.versym_section_index) |index| {3495 if (self.section_indexes.versym) |index| {
3518 const shdr = &slice.items(.shdr)[index];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 }
35213499
3522 if (self.verneed_section_index) |index| {3500 if (self.section_indexes.verneed) |index| {
3523 const shdr = &slice.items(.shdr)[index];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 }
35263504
3527 if (self.rela_dyn_section_index) |index| {3505 if (self.section_indexes.rela_dyn) |index| {
3528 const shdr = &slice.items(.shdr)[index];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 }
35313509
3532 if (self.rela_plt_section_index) |index| {3510 if (self.section_indexes.rela_plt) |index| {
3533 const shdr = &slice.items(.shdr)[index];3511 const shdr = &slice.items(.shdr)[index];
3534 shdr.sh_link = self.dynsymtab_section_index.?;3512 shdr.sh_link = self.section_indexes.dynsymtab.?;
3535 shdr.sh_info = self.plt_section_index.?;3513 shdr.sh_info = self.section_indexes.plt.?;
3536 }3514 }
35373515
3538 if (self.eh_frame_rela_section_index) |index| {3516 if (self.section_indexes.eh_frame_rela) |index| {
3539 const shdr = &slice.items(.shdr)[index];3517 const shdr = &slice.items(.shdr)[index];
3540 shdr.sh_link = self.symtab_section_index.?;3518 shdr.sh_link = self.section_indexes.symtab.?;
3541 shdr.sh_info = self.eh_frame_section_index.?;3519 shdr.sh_info = self.section_indexes.eh_frame.?;
3542 }3520 }
3543}3521}
35443522
...@@ -3572,31 +3550,31 @@ fn updateSectionSizes(self: *Elf) !void {...@@ -3572,31 +3550,31 @@ fn updateSectionSizes(self: *Elf) !void {
3572 }3550 }
35733551
3574 const shdrs = slice.items(.shdr);3552 const shdrs = slice.items(.shdr);
3575 if (self.eh_frame_section_index) |index| {3553 if (self.section_indexes.eh_frame) |index| {
3576 shdrs[index].sh_size = try eh_frame.calcEhFrameSize(self);3554 shdrs[index].sh_size = try eh_frame.calcEhFrameSize(self);
3577 }3555 }
35783556
3579 if (self.eh_frame_hdr_section_index) |index| {3557 if (self.section_indexes.eh_frame_hdr) |index| {
3580 shdrs[index].sh_size = eh_frame.calcEhFrameHdrSize(self);3558 shdrs[index].sh_size = eh_frame.calcEhFrameHdrSize(self);
3581 }3559 }
35823560
3583 if (self.got_section_index) |index| {3561 if (self.section_indexes.got) |index| {
3584 shdrs[index].sh_size = self.got.size(self);3562 shdrs[index].sh_size = self.got.size(self);
3585 }3563 }
35863564
3587 if (self.plt_section_index) |index| {3565 if (self.section_indexes.plt) |index| {
3588 shdrs[index].sh_size = self.plt.size(self);3566 shdrs[index].sh_size = self.plt.size(self);
3589 }3567 }
35903568
3591 if (self.got_plt_section_index) |index| {3569 if (self.section_indexes.got_plt) |index| {
3592 shdrs[index].sh_size = self.got_plt.size(self);3570 shdrs[index].sh_size = self.got_plt.size(self);
3593 }3571 }
35943572
3595 if (self.plt_got_section_index) |index| {3573 if (self.section_indexes.plt_got) |index| {
3596 shdrs[index].sh_size = self.plt_got.size(self);3574 shdrs[index].sh_size = self.plt_got.size(self);
3597 }3575 }
35983576
3599 if (self.rela_dyn_section_index) |shndx| {3577 if (self.section_indexes.rela_dyn) |shndx| {
3600 var num = self.got.numRela(self) + self.copy_rel.numRela();3578 var num = self.got.numRela(self) + self.copy_rel.numRela();
3601 if (self.zigObjectPtr()) |zig_object| {3579 if (self.zigObjectPtr()) |zig_object| {
3602 num += zig_object.num_dynrelocs;3580 num += zig_object.num_dynrelocs;
...@@ -3607,43 +3585,43 @@ fn updateSectionSizes(self: *Elf) !void {...@@ -3607,43 +3585,43 @@ fn updateSectionSizes(self: *Elf) !void {
3607 shdrs[shndx].sh_size = num * @sizeOf(elf.Elf64_Rela);3585 shdrs[shndx].sh_size = num * @sizeOf(elf.Elf64_Rela);
3608 }3586 }
36093587
3610 if (self.rela_plt_section_index) |index| {3588 if (self.section_indexes.rela_plt) |index| {
3611 shdrs[index].sh_size = self.plt.numRela() * @sizeOf(elf.Elf64_Rela);3589 shdrs[index].sh_size = self.plt.numRela() * @sizeOf(elf.Elf64_Rela);
3612 }3590 }
36133591
3614 if (self.copy_rel_section_index) |index| {3592 if (self.section_indexes.copy_rel) |index| {
3615 try self.copy_rel.updateSectionSize(index, self);3593 try self.copy_rel.updateSectionSize(index, self);
3616 }3594 }
36173595
3618 if (self.interp_section_index) |index| {3596 if (self.section_indexes.interp) |index| {
3619 shdrs[index].sh_size = self.getTarget().dynamic_linker.get().?.len + 1;3597 shdrs[index].sh_size = self.getTarget().dynamic_linker.get().?.len + 1;
3620 }3598 }
36213599
3622 if (self.hash_section_index) |index| {3600 if (self.section_indexes.hash) |index| {
3623 shdrs[index].sh_size = self.hash.size();3601 shdrs[index].sh_size = self.hash.size();
3624 }3602 }
36253603
3626 if (self.gnu_hash_section_index) |index| {3604 if (self.section_indexes.gnu_hash) |index| {
3627 shdrs[index].sh_size = self.gnu_hash.size();3605 shdrs[index].sh_size = self.gnu_hash.size();
3628 }3606 }
36293607
3630 if (self.dynamic_section_index) |index| {3608 if (self.section_indexes.dynamic) |index| {
3631 shdrs[index].sh_size = self.dynamic.size(self);3609 shdrs[index].sh_size = self.dynamic.size(self);
3632 }3610 }
36333611
3634 if (self.dynsymtab_section_index) |index| {3612 if (self.section_indexes.dynsymtab) |index| {
3635 shdrs[index].sh_size = self.dynsym.size();3613 shdrs[index].sh_size = self.dynsym.size();
3636 }3614 }
36373615
3638 if (self.dynstrtab_section_index) |index| {3616 if (self.section_indexes.dynstrtab) |index| {
3639 shdrs[index].sh_size = self.dynstrtab.items.len;3617 shdrs[index].sh_size = self.dynstrtab.items.len;
3640 }3618 }
36413619
3642 if (self.versym_section_index) |index| {3620 if (self.section_indexes.versym) |index| {
3643 shdrs[index].sh_size = self.versym.items.len * @sizeOf(elf.Elf64_Versym);3621 shdrs[index].sh_size = self.versym.items.len * @sizeOf(elf.Elf64_Versym);
3644 }3622 }
36453623
3646 if (self.verneed_section_index) |index| {3624 if (self.section_indexes.verneed) |index| {
3647 shdrs[index].sh_size = self.verneed.size();3625 shdrs[index].sh_size = self.verneed.size();
3648 }3626 }
36493627
...@@ -3653,7 +3631,7 @@ fn updateSectionSizes(self: *Elf) !void {...@@ -3653,7 +3631,7 @@ fn updateSectionSizes(self: *Elf) !void {
36533631
3654// FIXME:JK this is very much obsolete, remove!3632// FIXME:JK this is very much obsolete, remove!
3655pub fn updateShStrtabSize(self: *Elf) void {3633pub fn updateShStrtabSize(self: *Elf) void {
3656 if (self.shstrtab_section_index) |index| {3634 if (self.section_indexes.shstrtab) |index| {
3657 self.sections.items(.shdr)[index].sh_size = self.shstrtab.items.len;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,9 +3914,9 @@ fn allocateSpecialPhdrs(self: *Elf) void {
3936 const slice = self.sections.slice();3914 const slice = self.sections.slice();
39373915
3938 for (&[_]struct { OptionalProgramHeaderIndex, ?u32 }{3916 for (&[_]struct { OptionalProgramHeaderIndex, ?u32 }{
3939 .{ self.phdr_indexes.interp, self.interp_section_index },3917 .{ self.phdr_indexes.interp, self.section_indexes.interp },
3940 .{ self.phdr_indexes.dynamic, self.dynamic_section_index },3918 .{ self.phdr_indexes.dynamic, self.section_indexes.dynamic },
3941 .{ self.phdr_indexes.gnu_eh_frame, self.eh_frame_hdr_section_index },3919 .{ self.phdr_indexes.gnu_eh_frame, self.section_indexes.eh_frame_hdr },
3942 }) |pair| {3920 }) |pair| {
3943 if (pair[0].int()) |index| {3921 if (pair[0].int()) |index| {
3944 const shdr = slice.items(.shdr)[pair[1].?];3922 const shdr = slice.items(.shdr)[pair[1].?];
...@@ -4075,7 +4053,7 @@ pub fn updateSymtabSize(self: *Elf) !void {...@@ -4075,7 +4053,7 @@ pub fn updateSymtabSize(self: *Elf) !void {
4075 strsize += ctx.strsize;4053 strsize += ctx.strsize;
4076 }4054 }
40774055
4078 if (self.got_section_index) |_| {4056 if (self.section_indexes.got) |_| {
4079 self.got.output_symtab_ctx.reset();4057 self.got.output_symtab_ctx.reset();
4080 self.got.output_symtab_ctx.ilocal = nlocals;4058 self.got.output_symtab_ctx.ilocal = nlocals;
4081 self.got.updateSymtabSize(self);4059 self.got.updateSymtabSize(self);
...@@ -4083,7 +4061,7 @@ pub fn updateSymtabSize(self: *Elf) !void {...@@ -4083,7 +4061,7 @@ pub fn updateSymtabSize(self: *Elf) !void {
4083 strsize += self.got.output_symtab_ctx.strsize;4061 strsize += self.got.output_symtab_ctx.strsize;
4084 }4062 }
40854063
4086 if (self.plt_section_index) |_| {4064 if (self.section_indexes.plt) |_| {
4087 self.plt.output_symtab_ctx.reset();4065 self.plt.output_symtab_ctx.reset();
4088 self.plt.output_symtab_ctx.ilocal = nlocals;4066 self.plt.output_symtab_ctx.ilocal = nlocals;
4089 self.plt.updateSymtabSize(self);4067 self.plt.updateSymtabSize(self);
...@@ -4091,7 +4069,7 @@ pub fn updateSymtabSize(self: *Elf) !void {...@@ -4091,7 +4069,7 @@ pub fn updateSymtabSize(self: *Elf) !void {
4091 strsize += self.plt.output_symtab_ctx.strsize;4069 strsize += self.plt.output_symtab_ctx.strsize;
4092 }4070 }
40934071
4094 if (self.plt_got_section_index) |_| {4072 if (self.section_indexes.plt_got) |_| {
4095 self.plt_got.output_symtab_ctx.reset();4073 self.plt_got.output_symtab_ctx.reset();
4096 self.plt_got.output_symtab_ctx.ilocal = nlocals;4074 self.plt_got.output_symtab_ctx.ilocal = nlocals;
4097 self.plt_got.updateSymtabSize(self);4075 self.plt_got.updateSymtabSize(self);
...@@ -4108,9 +4086,9 @@ pub fn updateSymtabSize(self: *Elf) !void {...@@ -4108,9 +4086,9 @@ pub fn updateSymtabSize(self: *Elf) !void {
4108 }4086 }
41094087
4110 const slice = self.sections.slice();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 symtab_shdr.sh_info = nlocals;4090 symtab_shdr.sh_info = nlocals;
4113 symtab_shdr.sh_link = self.strtab_section_index.?;4091 symtab_shdr.sh_link = self.section_indexes.strtab.?;
41144092
4115 const sym_size: u64 = switch (self.ptr_width) {4093 const sym_size: u64 = switch (self.ptr_width) {
4116 .p32 => @sizeOf(elf.Elf32_Sym),4094 .p32 => @sizeOf(elf.Elf32_Sym),
...@@ -4119,7 +4097,7 @@ pub fn updateSymtabSize(self: *Elf) !void {...@@ -4119,7 +4097,7 @@ pub fn updateSymtabSize(self: *Elf) !void {
4119 const needed_size = (nlocals + nglobals) * sym_size;4097 const needed_size = (nlocals + nglobals) * sym_size;
4120 symtab_shdr.sh_size = needed_size;4098 symtab_shdr.sh_size = needed_size;
41214099
4122 const strtab = &slice.items(.shdr)[self.strtab_section_index.?];4100 const strtab = &slice.items(.shdr)[self.section_indexes.strtab.?];
4123 strtab.sh_size = strsize + 1;4101 strtab.sh_size = strsize + 1;
4124}4102}
41254103
...@@ -4127,7 +4105,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4127,7 +4105,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4127 const gpa = self.base.comp.gpa;4105 const gpa = self.base.comp.gpa;
4128 const slice = self.sections.slice();4106 const slice = self.sections.slice();
41294107
4130 if (self.interp_section_index) |shndx| {4108 if (self.section_indexes.interp) |shndx| {
4131 var buffer: [256]u8 = undefined;4109 var buffer: [256]u8 = undefined;
4132 const interp = self.getTarget().dynamic_linker.get().?;4110 const interp = self.getTarget().dynamic_linker.get().?;
4133 @memcpy(buffer[0..interp.len], interp);4111 @memcpy(buffer[0..interp.len], interp);
...@@ -4138,12 +4116,12 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4138,12 +4116,12 @@ fn writeSyntheticSections(self: *Elf) !void {
4138 try self.base.file.?.pwriteAll(contents, shdr.sh_offset);4116 try self.base.file.?.pwriteAll(contents, shdr.sh_offset);
4139 }4117 }
41404118
4141 if (self.hash_section_index) |shndx| {4119 if (self.section_indexes.hash) |shndx| {
4142 const shdr = slice.items(.shdr)[shndx];4120 const shdr = slice.items(.shdr)[shndx];
4143 try self.base.file.?.pwriteAll(self.hash.buffer.items, shdr.sh_offset);4121 try self.base.file.?.pwriteAll(self.hash.buffer.items, shdr.sh_offset);
4144 }4122 }
41454123
4146 if (self.gnu_hash_section_index) |shndx| {4124 if (self.section_indexes.gnu_hash) |shndx| {
4147 const shdr = slice.items(.shdr)[shndx];4125 const shdr = slice.items(.shdr)[shndx];
4148 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.gnu_hash.size());4126 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.gnu_hash.size());
4149 defer buffer.deinit();4127 defer buffer.deinit();
...@@ -4151,12 +4129,12 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4151,12 +4129,12 @@ fn writeSyntheticSections(self: *Elf) !void {
4151 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4129 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
4152 }4130 }
41534131
4154 if (self.versym_section_index) |shndx| {4132 if (self.section_indexes.versym) |shndx| {
4155 const shdr = slice.items(.shdr)[shndx];4133 const shdr = slice.items(.shdr)[shndx];
4156 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.versym.items), shdr.sh_offset);4134 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.versym.items), shdr.sh_offset);
4157 }4135 }
41584136
4159 if (self.verneed_section_index) |shndx| {4137 if (self.section_indexes.verneed) |shndx| {
4160 const shdr = slice.items(.shdr)[shndx];4138 const shdr = slice.items(.shdr)[shndx];
4161 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.verneed.size());4139 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.verneed.size());
4162 defer buffer.deinit();4140 defer buffer.deinit();
...@@ -4164,7 +4142,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4164,7 +4142,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4164 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4142 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
4165 }4143 }
41664144
4167 if (self.dynamic_section_index) |shndx| {4145 if (self.section_indexes.dynamic) |shndx| {
4168 const shdr = slice.items(.shdr)[shndx];4146 const shdr = slice.items(.shdr)[shndx];
4169 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.dynamic.size(self));4147 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.dynamic.size(self));
4170 defer buffer.deinit();4148 defer buffer.deinit();
...@@ -4172,7 +4150,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4172,7 +4150,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4172 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4150 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
4173 }4151 }
41744152
4175 if (self.dynsymtab_section_index) |shndx| {4153 if (self.section_indexes.dynsymtab) |shndx| {
4176 const shdr = slice.items(.shdr)[shndx];4154 const shdr = slice.items(.shdr)[shndx];
4177 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.dynsym.size());4155 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.dynsym.size());
4178 defer buffer.deinit();4156 defer buffer.deinit();
...@@ -4180,12 +4158,12 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4180,12 +4158,12 @@ fn writeSyntheticSections(self: *Elf) !void {
4180 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4158 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
4181 }4159 }
41824160
4183 if (self.dynstrtab_section_index) |shndx| {4161 if (self.section_indexes.dynstrtab) |shndx| {
4184 const shdr = slice.items(.shdr)[shndx];4162 const shdr = slice.items(.shdr)[shndx];
4185 try self.base.file.?.pwriteAll(self.dynstrtab.items, shdr.sh_offset);4163 try self.base.file.?.pwriteAll(self.dynstrtab.items, shdr.sh_offset);
4186 }4164 }
41874165
4188 if (self.eh_frame_section_index) |shndx| {4166 if (self.section_indexes.eh_frame) |shndx| {
4189 const existing_size = existing_size: {4167 const existing_size = existing_size: {
4190 const zo = self.zigObjectPtr() orelse break :existing_size 0;4168 const zo = self.zigObjectPtr() orelse break :existing_size 0;
4191 const sym = zo.symbol(zo.eh_frame_index orelse break :existing_size 0);4169 const sym = zo.symbol(zo.eh_frame_index orelse break :existing_size 0);
...@@ -4200,7 +4178,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4200,7 +4178,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4200 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset + existing_size);4178 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset + existing_size);
4201 }4179 }
42024180
4203 if (self.eh_frame_hdr_section_index) |shndx| {4181 if (self.section_indexes.eh_frame_hdr) |shndx| {
4204 const shdr = slice.items(.shdr)[shndx];4182 const shdr = slice.items(.shdr)[shndx];
4205 const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;4183 const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
4206 var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size);4184 var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size);
...@@ -4209,7 +4187,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4209,7 +4187,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4209 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4187 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
4210 }4188 }
42114189
4212 if (self.got_section_index) |index| {4190 if (self.section_indexes.got) |index| {
4213 const shdr = slice.items(.shdr)[index];4191 const shdr = slice.items(.shdr)[index];
4214 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.got.size(self));4192 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.got.size(self));
4215 defer buffer.deinit();4193 defer buffer.deinit();
...@@ -4217,7 +4195,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4217,7 +4195,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4217 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4195 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
4218 }4196 }
42194197
4220 if (self.rela_dyn_section_index) |shndx| {4198 if (self.section_indexes.rela_dyn) |shndx| {
4221 const shdr = slice.items(.shdr)[shndx];4199 const shdr = slice.items(.shdr)[shndx];
4222 try self.got.addRela(self);4200 try self.got.addRela(self);
4223 try self.copy_rel.addRela(self);4201 try self.copy_rel.addRela(self);
...@@ -4225,7 +4203,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4225,7 +4203,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4225 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_dyn.items), shdr.sh_offset);4203 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_dyn.items), shdr.sh_offset);
4226 }4204 }
42274205
4228 if (self.plt_section_index) |shndx| {4206 if (self.section_indexes.plt) |shndx| {
4229 const shdr = slice.items(.shdr)[shndx];4207 const shdr = slice.items(.shdr)[shndx];
4230 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.plt.size(self));4208 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.plt.size(self));
4231 defer buffer.deinit();4209 defer buffer.deinit();
...@@ -4233,7 +4211,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4233,7 +4211,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4233 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4211 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
4234 }4212 }
42354213
4236 if (self.got_plt_section_index) |shndx| {4214 if (self.section_indexes.got_plt) |shndx| {
4237 const shdr = slice.items(.shdr)[shndx];4215 const shdr = slice.items(.shdr)[shndx];
4238 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.got_plt.size(self));4216 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.got_plt.size(self));
4239 defer buffer.deinit();4217 defer buffer.deinit();
...@@ -4241,7 +4219,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4241,7 +4219,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4241 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4219 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
4242 }4220 }
42434221
4244 if (self.plt_got_section_index) |shndx| {4222 if (self.section_indexes.plt_got) |shndx| {
4245 const shdr = slice.items(.shdr)[shndx];4223 const shdr = slice.items(.shdr)[shndx];
4246 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.plt_got.size(self));4224 var buffer = try std.ArrayList(u8).initCapacity(gpa, self.plt_got.size(self));
4247 defer buffer.deinit();4225 defer buffer.deinit();
...@@ -4249,7 +4227,7 @@ fn writeSyntheticSections(self: *Elf) !void {...@@ -4249,7 +4227,7 @@ fn writeSyntheticSections(self: *Elf) !void {
4249 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);4227 try self.base.file.?.pwriteAll(buffer.items, shdr.sh_offset);
4250 }4228 }
42514229
4252 if (self.rela_plt_section_index) |shndx| {4230 if (self.section_indexes.rela_plt) |shndx| {
4253 const shdr = slice.items(.shdr)[shndx];4231 const shdr = slice.items(.shdr)[shndx];
4254 try self.plt.addRela(self);4232 try self.plt.addRela(self);
4255 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.rela_plt.items), shdr.sh_offset);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,7 +4239,7 @@ fn writeSyntheticSections(self: *Elf) !void {
42614239
4262// FIXME:JK again, why is this needed?4240// FIXME:JK again, why is this needed?
4263pub fn writeShStrtab(self: *Elf) !void {4241pub fn writeShStrtab(self: *Elf) !void {
4264 if (self.shstrtab_section_index) |index| {4242 if (self.section_indexes.shstrtab) |index| {
4265 const shdr = self.sections.items(.shdr)[index];4243 const shdr = self.sections.items(.shdr)[index];
4266 log.debug("writing .shstrtab from 0x{x} to 0x{x}", .{ shdr.sh_offset, shdr.sh_offset + shdr.sh_size });4244 log.debug("writing .shstrtab from 0x{x} to 0x{x}", .{ shdr.sh_offset, shdr.sh_offset + shdr.sh_size });
4267 try self.base.file.?.pwriteAll(self.shstrtab.items, shdr.sh_offset);4245 try self.base.file.?.pwriteAll(self.shstrtab.items, shdr.sh_offset);
...@@ -4271,8 +4249,8 @@ pub fn writeShStrtab(self: *Elf) !void {...@@ -4271,8 +4249,8 @@ pub fn writeShStrtab(self: *Elf) !void {
4271pub fn writeSymtab(self: *Elf) !void {4249pub fn writeSymtab(self: *Elf) !void {
4272 const gpa = self.base.comp.gpa;4250 const gpa = self.base.comp.gpa;
4273 const slice = self.sections.slice();4251 const slice = self.sections.slice();
4274 const symtab_shdr = slice.items(.shdr)[self.symtab_section_index.?];4252 const symtab_shdr = slice.items(.shdr)[self.section_indexes.symtab.?];
4275 const strtab_shdr = slice.items(.shdr)[self.strtab_section_index.?];4253 const strtab_shdr = slice.items(.shdr)[self.section_indexes.strtab.?];
4276 const sym_size: u64 = switch (self.ptr_width) {4254 const sym_size: u64 = switch (self.ptr_width) {
4277 .p32 => @sizeOf(elf.Elf32_Sym),4255 .p32 => @sizeOf(elf.Elf32_Sym),
4278 .p64 => @sizeOf(elf.Elf64_Sym),4256 .p64 => @sizeOf(elf.Elf64_Sym),
...@@ -4330,15 +4308,15 @@ pub fn writeSymtab(self: *Elf) !void {...@@ -4330,15 +4308,15 @@ pub fn writeSymtab(self: *Elf) !void {
4330 obj.asFile().writeSymtab(self);4308 obj.asFile().writeSymtab(self);
4331 }4309 }
43324310
4333 if (self.got_section_index) |_| {4311 if (self.section_indexes.got) |_| {
4334 self.got.writeSymtab(self);4312 self.got.writeSymtab(self);
4335 }4313 }
43364314
4337 if (self.plt_section_index) |_| {4315 if (self.section_indexes.plt) |_| {
4338 self.plt.writeSymtab(self);4316 self.plt.writeSymtab(self);
4339 }4317 }
43404318
4341 if (self.plt_got_section_index) |_| {4319 if (self.section_indexes.plt_got) |_| {
4342 self.plt_got.writeSymtab(self);4320 self.plt_got.writeSymtab(self);
4343 }4321 }
43444322
...@@ -4778,9 +4756,9 @@ pub fn mergeSection(self: *Elf, index: MergeSection.Index) *MergeSection {...@@ -4778,9 +4756,9 @@ pub fn mergeSection(self: *Elf, index: MergeSection.Index) *MergeSection {
47784756
4779pub fn gotAddress(self: *Elf) i64 {4757pub fn gotAddress(self: *Elf) i64 {
4780 const shndx = blk: {4758 const shndx = blk: {
4781 if (self.getTarget().cpu.arch == .x86_64 and self.got_plt_section_index != null)4759 if (self.getTarget().cpu.arch == .x86_64 and self.section_indexes.got_plt != null)
4782 break :blk self.got_plt_section_index.?;4760 break :blk self.section_indexes.got_plt.?;
4783 break :blk if (self.got_section_index) |shndx| shndx else null;4761 break :blk if (self.section_indexes.got) |shndx| shndx else null;
4784 };4762 };
4785 return if (shndx) |index| @intCast(self.sections.items(.shdr)[index].sh_addr) else 0;4763 return if (shndx) |index| @intCast(self.sections.items(.shdr)[index].sh_addr) else 0;
4786}4764}
src/link/Elf/LinkerDefined.zig+6-6
...@@ -205,7 +205,7 @@ pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {...@@ -205,7 +205,7 @@ pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {
205 }.allocSymbol;205 }.allocSymbol;
206206
207 // _DYNAMIC207 // _DYNAMIC
208 if (elf_file.dynamic_section_index) |shndx| {208 if (elf_file.section_indexes.dynamic) |shndx| {
209 const shdr = shdrs[shndx];209 const shdr = shdrs[shndx];
210 allocSymbol(self, self.dynamic_index.?, shdr.sh_addr, shndx, elf_file);210 allocSymbol(self, self.dynamic_index.?, shdr.sh_addr, shndx, elf_file);
211 }211 }
...@@ -236,19 +236,19 @@ pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {...@@ -236,19 +236,19 @@ pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {
236236
237 // _GLOBAL_OFFSET_TABLE_237 // _GLOBAL_OFFSET_TABLE_
238 if (elf_file.getTarget().cpu.arch == .x86_64) {238 if (elf_file.getTarget().cpu.arch == .x86_64) {
239 if (elf_file.got_plt_section_index) |shndx| {239 if (elf_file.section_indexes.got_plt) |shndx| {
240 const shdr = shdrs[shndx];240 const shdr = shdrs[shndx];
241 allocSymbol(self, self.got_index.?, shdr.sh_addr, shndx, elf_file);241 allocSymbol(self, self.got_index.?, shdr.sh_addr, shndx, elf_file);
242 }242 }
243 } else {243 } else {
244 if (elf_file.got_section_index) |shndx| {244 if (elf_file.section_indexes.got) |shndx| {
245 const shdr = shdrs[shndx];245 const shdr = shdrs[shndx];
246 allocSymbol(self, self.got_index.?, shdr.sh_addr, shndx, elf_file);246 allocSymbol(self, self.got_index.?, shdr.sh_addr, shndx, elf_file);
247 }247 }
248 }248 }
249249
250 // _PROCEDURE_LINKAGE_TABLE_250 // _PROCEDURE_LINKAGE_TABLE_
251 if (elf_file.plt_section_index) |shndx| {251 if (elf_file.section_indexes.plt) |shndx| {
252 const shdr = shdrs[shndx];252 const shdr = shdrs[shndx];
253 allocSymbol(self, self.plt_index.?, shdr.sh_addr, shndx, elf_file);253 allocSymbol(self, self.plt_index.?, shdr.sh_addr, shndx, elf_file);
254 }254 }
...@@ -262,13 +262,13 @@ pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {...@@ -262,13 +262,13 @@ pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {
262 }262 }
263263
264 // __GNU_EH_FRAME_HDR264 // __GNU_EH_FRAME_HDR
265 if (elf_file.eh_frame_hdr_section_index) |shndx| {265 if (elf_file.section_indexes.eh_frame_hdr) |shndx| {
266 const shdr = shdrs[shndx];266 const shdr = shdrs[shndx];
267 allocSymbol(self, self.gnu_eh_frame_hdr_index.?, shdr.sh_addr, shndx, elf_file);267 allocSymbol(self, self.gnu_eh_frame_hdr_index.?, shdr.sh_addr, shndx, elf_file);
268 }268 }
269269
270 // __rela_iplt_start, __rela_iplt_end270 // __rela_iplt_start, __rela_iplt_end
271 if (elf_file.rela_dyn_section_index) |shndx| blk: {271 if (elf_file.section_indexes.rela_dyn) |shndx| blk: {
272 if (link_mode != .static or comp.config.pie) break :blk;272 if (link_mode != .static or comp.config.pie) break :blk;
273 const shdr = shdrs[shndx];273 const shdr = shdrs[shndx];
274 const end_addr = shdr.sh_addr + shdr.sh_size;274 const end_addr = shdr.sh_addr + shdr.sh_size;
src/link/Elf/Object.zig+3-3
...@@ -927,7 +927,7 @@ pub fn initRelaSections(self: *Object, elf_file: *Elf) !void {...@@ -927,7 +927,7 @@ pub fn initRelaSections(self: *Object, elf_file: *Elf) !void {
927 for (self.atoms_indexes.items) |atom_index| {927 for (self.atoms_indexes.items) |atom_index| {
928 const atom_ptr = self.atom(atom_index) orelse continue;928 const atom_ptr = self.atom(atom_index) orelse continue;
929 if (!atom_ptr.alive) continue;929 if (!atom_ptr.alive) continue;
930 if (atom_ptr.output_section_index == elf_file.eh_frame_section_index) continue;930 if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
931 const shndx = atom_ptr.relocsShndx() orelse continue;931 const shndx = atom_ptr.relocsShndx() orelse continue;
932 const shdr = self.shdrs.items[shndx];932 const shdr = self.shdrs.items[shndx];
933 const out_shndx = try elf_file.initOutputSection(.{933 const out_shndx = try elf_file.initOutputSection(.{
...@@ -947,7 +947,7 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {...@@ -947,7 +947,7 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {
947 for (self.atoms_indexes.items) |atom_index| {947 for (self.atoms_indexes.items) |atom_index| {
948 const atom_ptr = self.atom(atom_index) orelse continue;948 const atom_ptr = self.atom(atom_index) orelse continue;
949 if (!atom_ptr.alive) continue;949 if (!atom_ptr.alive) continue;
950 if (atom_ptr.output_section_index == elf_file.eh_frame_section_index) continue;950 if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
951 const shndx = blk: {951 const shndx = blk: {
952 const shndx = atom_ptr.relocsShndx() orelse continue;952 const shndx = atom_ptr.relocsShndx() orelse continue;
953 const shdr = self.shdrs.items[shndx];953 const shdr = self.shdrs.items[shndx];
...@@ -960,7 +960,7 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {...@@ -960,7 +960,7 @@ pub fn addAtomsToRelaSections(self: *Object, elf_file: *Elf) !void {
960 const slice = elf_file.sections.slice();960 const slice = elf_file.sections.slice();
961 const shdr = &slice.items(.shdr)[shndx];961 const shdr = &slice.items(.shdr)[shndx];
962 shdr.sh_info = atom_ptr.output_section_index;962 shdr.sh_info = atom_ptr.output_section_index;
963 shdr.sh_link = elf_file.symtab_section_index.?;963 shdr.sh_link = elf_file.section_indexes.symtab.?;
964 const gpa = elf_file.base.comp.gpa;964 const gpa = elf_file.base.comp.gpa;
965 const atom_list = &elf_file.sections.items(.atom_list)[shndx];965 const atom_list = &elf_file.sections.items(.atom_list)[shndx];
966 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });966 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
src/link/Elf/Symbol.zig+6-6
...@@ -128,7 +128,7 @@ pub fn address(symbol: Symbol, opts: struct { plt: bool = true, trampoline: bool...@@ -128,7 +128,7 @@ pub fn address(symbol: Symbol, opts: struct { plt: bool = true, trampoline: bool
128 if (mem.eql(u8, atom_ptr.name(elf_file), ".eh_frame")) {128 if (mem.eql(u8, atom_ptr.name(elf_file), ".eh_frame")) {
129 const sym_name = symbol.name(elf_file);129 const sym_name = symbol.name(elf_file);
130 const sh_addr, const sh_size = blk: {130 const sh_addr, const sh_size = blk: {
131 const shndx = elf_file.eh_frame_section_index orelse break :blk .{ 0, 0 };131 const shndx = elf_file.section_indexes.eh_frame orelse break :blk .{ 0, 0 };
132 const shdr = elf_file.sections.items(.shdr)[shndx];132 const shdr = elf_file.sections.items(.shdr)[shndx];
133 break :blk .{ shdr.sh_addr, shdr.sh_size };133 break :blk .{ shdr.sh_addr, shdr.sh_size };
134 };134 };
...@@ -176,7 +176,7 @@ pub fn gotAddress(symbol: Symbol, elf_file: *Elf) i64 {...@@ -176,7 +176,7 @@ pub fn gotAddress(symbol: Symbol, elf_file: *Elf) i64 {
176pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) i64 {176pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) i64 {
177 if (!symbol.flags.has_pltgot) return 0;177 if (!symbol.flags.has_pltgot) return 0;
178 const extras = symbol.extra(elf_file);178 const extras = symbol.extra(elf_file);
179 const shdr = elf_file.sections.items(.shdr)[elf_file.plt_got_section_index.?];179 const shdr = elf_file.sections.items(.shdr)[elf_file.section_indexes.plt_got.?];
180 const cpu_arch = elf_file.getTarget().cpu.arch;180 const cpu_arch = elf_file.getTarget().cpu.arch;
181 return @intCast(shdr.sh_addr + extras.plt_got * PltGotSection.entrySize(cpu_arch));181 return @intCast(shdr.sh_addr + extras.plt_got * PltGotSection.entrySize(cpu_arch));
182}182}
...@@ -184,7 +184,7 @@ pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) i64 {...@@ -184,7 +184,7 @@ pub fn pltGotAddress(symbol: Symbol, elf_file: *Elf) i64 {
184pub fn pltAddress(symbol: Symbol, elf_file: *Elf) i64 {184pub fn pltAddress(symbol: Symbol, elf_file: *Elf) i64 {
185 if (!symbol.flags.has_plt) return 0;185 if (!symbol.flags.has_plt) return 0;
186 const extras = symbol.extra(elf_file);186 const extras = symbol.extra(elf_file);
187 const shdr = elf_file.sections.items(.shdr)[elf_file.plt_section_index.?];187 const shdr = elf_file.sections.items(.shdr)[elf_file.section_indexes.plt.?];
188 const cpu_arch = elf_file.getTarget().cpu.arch;188 const cpu_arch = elf_file.getTarget().cpu.arch;
189 return @intCast(shdr.sh_addr + extras.plt * PltSection.entrySize(cpu_arch) + PltSection.preambleSize(cpu_arch));189 return @intCast(shdr.sh_addr + extras.plt * PltSection.entrySize(cpu_arch) + PltSection.preambleSize(cpu_arch));
190}190}
...@@ -192,13 +192,13 @@ pub fn pltAddress(symbol: Symbol, elf_file: *Elf) i64 {...@@ -192,13 +192,13 @@ pub fn pltAddress(symbol: Symbol, elf_file: *Elf) i64 {
192pub fn gotPltAddress(symbol: Symbol, elf_file: *Elf) i64 {192pub fn gotPltAddress(symbol: Symbol, elf_file: *Elf) i64 {
193 if (!symbol.flags.has_plt) return 0;193 if (!symbol.flags.has_plt) return 0;
194 const extras = symbol.extra(elf_file);194 const extras = symbol.extra(elf_file);
195 const shdr = elf_file.sections.items(.shdr)[elf_file.got_plt_section_index.?];195 const shdr = elf_file.sections.items(.shdr)[elf_file.section_indexes.got_plt.?];
196 return @intCast(shdr.sh_addr + extras.plt * 8 + GotPltSection.preamble_size);196 return @intCast(shdr.sh_addr + extras.plt * 8 + GotPltSection.preamble_size);
197}197}
198198
199pub fn copyRelAddress(symbol: Symbol, elf_file: *Elf) i64 {199pub fn copyRelAddress(symbol: Symbol, elf_file: *Elf) i64 {
200 if (!symbol.flags.has_copy_rel) return 0;200 if (!symbol.flags.has_copy_rel) return 0;
201 const shdr = elf_file.sections.items(.shdr)[elf_file.copy_rel_section_index.?];201 const shdr = elf_file.sections.items(.shdr)[elf_file.section_indexes.copy_rel.?];
202 return @as(i64, @intCast(shdr.sh_addr)) + symbol.value;202 return @as(i64, @intCast(shdr.sh_addr)) + symbol.value;
203}203}
204204
...@@ -289,7 +289,7 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {...@@ -289,7 +289,7 @@ pub fn setOutputSym(symbol: Symbol, elf_file: *Elf, out: *elf.Elf64_Sym) void {
289 break :blk esym.st_bind();289 break :blk esym.st_bind();
290 };290 };
291 const st_shndx: u16 = blk: {291 const st_shndx: u16 = blk: {
292 if (symbol.flags.has_copy_rel) break :blk @intCast(elf_file.copy_rel_section_index.?);292 if (symbol.flags.has_copy_rel) break :blk @intCast(elf_file.section_indexes.copy_rel.?);
293 if (file_ptr == .shared_object or esym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF;293 if (file_ptr == .shared_object or esym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF;
294 if (elf_file.base.isRelocatable() and esym.st_shndx == elf.SHN_COMMON) break :blk elf.SHN_COMMON;294 if (elf_file.base.isRelocatable() and esym.st_shndx == elf.SHN_COMMON) break :blk elf.SHN_COMMON;
295 if (symbol.mergeSubsection(elf_file)) |msub| break :blk @intCast(msub.mergeSection(elf_file).output_section_index);295 if (symbol.mergeSubsection(elf_file)) |msub| break :blk @intCast(msub.mergeSection(elf_file).output_section_index);
src/link/Elf/ZigObject.zig+3-3
...@@ -791,7 +791,7 @@ pub fn initRelaSections(self: *ZigObject, elf_file: *Elf) !void {...@@ -791,7 +791,7 @@ pub fn initRelaSections(self: *ZigObject, elf_file: *Elf) !void {
791 for (self.atoms_indexes.items) |atom_index| {791 for (self.atoms_indexes.items) |atom_index| {
792 const atom_ptr = self.atom(atom_index) orelse continue;792 const atom_ptr = self.atom(atom_index) orelse continue;
793 if (!atom_ptr.alive) continue;793 if (!atom_ptr.alive) continue;
794 if (atom_ptr.output_section_index == elf_file.eh_frame_section_index) continue;794 if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
795 const rela_shndx = atom_ptr.relocsShndx() orelse continue;795 const rela_shndx = atom_ptr.relocsShndx() orelse continue;
796 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level796 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
797 if (self.relocs.items[rela_shndx].items.len == 0) continue;797 if (self.relocs.items[rela_shndx].items.len == 0) continue;
...@@ -812,7 +812,7 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {...@@ -812,7 +812,7 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
812 for (self.atoms_indexes.items) |atom_index| {812 for (self.atoms_indexes.items) |atom_index| {
813 const atom_ptr = self.atom(atom_index) orelse continue;813 const atom_ptr = self.atom(atom_index) orelse continue;
814 if (!atom_ptr.alive) continue;814 if (!atom_ptr.alive) continue;
815 if (atom_ptr.output_section_index == elf_file.eh_frame_section_index) continue;815 if (atom_ptr.output_section_index == elf_file.section_indexes.eh_frame) continue;
816 const rela_shndx = atom_ptr.relocsShndx() orelse continue;816 const rela_shndx = atom_ptr.relocsShndx() orelse continue;
817 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level817 // TODO this check will become obsolete when we rework our relocs mechanism at the ZigObject level
818 if (self.relocs.items[rela_shndx].items.len == 0) continue;818 if (self.relocs.items[rela_shndx].items.len == 0) continue;
...@@ -826,7 +826,7 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {...@@ -826,7 +826,7 @@ pub fn addAtomsToRelaSections(self: *ZigObject, elf_file: *Elf) !void {
826 const out_rela_shndx = elf_file.sectionByName(rela_sect_name).?;826 const out_rela_shndx = elf_file.sectionByName(rela_sect_name).?;
827 const out_rela_shdr = &elf_file.sections.items(.shdr)[out_rela_shndx];827 const out_rela_shdr = &elf_file.sections.items(.shdr)[out_rela_shndx];
828 out_rela_shdr.sh_info = out_shndx;828 out_rela_shdr.sh_info = out_shndx;
829 out_rela_shdr.sh_link = elf_file.symtab_section_index.?;829 out_rela_shdr.sh_link = elf_file.section_indexes.symtab.?;
830 const atom_list = &elf_file.sections.items(.atom_list)[out_rela_shndx];830 const atom_list = &elf_file.sections.items(.atom_list)[out_rela_shndx];
831 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });831 try atom_list.append(gpa, .{ .index = atom_index, .file = self.index });
832 }832 }
src/link/Elf/eh_frame.zig+6-6
...@@ -12,7 +12,7 @@ pub const Fde = struct {...@@ -12,7 +12,7 @@ pub const Fde = struct {
12 out_offset: u64 = 0,12 out_offset: u64 = 0,
1313
14 pub fn address(fde: Fde, elf_file: *Elf) u64 {14 pub fn address(fde: Fde, elf_file: *Elf) u64 {
15 const base: u64 = if (elf_file.eh_frame_section_index) |shndx|15 const base: u64 = if (elf_file.section_indexes.eh_frame) |shndx|
16 elf_file.sections.items(.shdr)[shndx].sh_addr16 elf_file.sections.items(.shdr)[shndx].sh_addr
17 else17 else
18 0;18 0;
...@@ -111,7 +111,7 @@ pub const Cie = struct {...@@ -111,7 +111,7 @@ pub const Cie = struct {
111 alive: bool = false,111 alive: bool = false,
112112
113 pub fn address(cie: Cie, elf_file: *Elf) u64 {113 pub fn address(cie: Cie, elf_file: *Elf) u64 {
114 const base: u64 = if (elf_file.eh_frame_section_index) |shndx|114 const base: u64 = if (elf_file.section_indexes.eh_frame) |shndx|
115 elf_file.sections.items(.shdr)[shndx].sh_addr115 elf_file.sections.items(.shdr)[shndx].sh_addr
116 else116 else
117 0;117 0;
...@@ -337,7 +337,7 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file:...@@ -337,7 +337,7 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file:
337337
338pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {338pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
339 relocs_log.debug("{x}: .eh_frame", .{339 relocs_log.debug("{x}: .eh_frame", .{
340 elf_file.sections.items(.shdr)[elf_file.eh_frame_section_index.?].sh_addr,340 elf_file.sections.items(.shdr)[elf_file.section_indexes.eh_frame.?].sh_addr,
341 });341 });
342342
343 var has_reloc_errors = false;343 var has_reloc_errors = false;
...@@ -458,7 +458,7 @@ fn emitReloc(elf_file: *Elf, r_offset: u64, sym: *const Symbol, rel: elf.Elf64_R...@@ -458,7 +458,7 @@ fn emitReloc(elf_file: *Elf, r_offset: u64, sym: *const Symbol, rel: elf.Elf64_R
458458
459pub fn writeEhFrameRelocs(elf_file: *Elf, writer: anytype) !void {459pub fn writeEhFrameRelocs(elf_file: *Elf, writer: anytype) !void {
460 relocs_log.debug("{x}: .eh_frame", .{460 relocs_log.debug("{x}: .eh_frame", .{
461 elf_file.sections.items(.shdr)[elf_file.eh_frame_section_index.?].sh_addr,461 elf_file.sections.items(.shdr)[elf_file.section_indexes.eh_frame.?].sh_addr,
462 });462 });
463463
464 if (elf_file.zigObjectPtr()) |zo| zo: {464 if (elf_file.zigObjectPtr()) |zo| zo: {
...@@ -511,8 +511,8 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {...@@ -511,8 +511,8 @@ pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
511 try writer.writeByte(DW_EH_PE.datarel | DW_EH_PE.sdata4);511 try writer.writeByte(DW_EH_PE.datarel | DW_EH_PE.sdata4);
512512
513 const shdrs = elf_file.sections.items(.shdr);513 const shdrs = elf_file.sections.items(.shdr);
514 const eh_frame_shdr = shdrs[elf_file.eh_frame_section_index.?];514 const eh_frame_shdr = shdrs[elf_file.section_indexes.eh_frame.?];
515 const eh_frame_hdr_shdr = shdrs[elf_file.eh_frame_hdr_section_index.?];515 const eh_frame_hdr_shdr = shdrs[elf_file.section_indexes.eh_frame_hdr.?];
516 const num_fdes = @as(u32, @intCast(@divExact(eh_frame_hdr_shdr.sh_size - eh_frame_hdr_header_size, 8)));516 const num_fdes = @as(u32, @intCast(@divExact(eh_frame_hdr_shdr.sh_size - eh_frame_hdr_header_size, 8)));
517 const existing_size = existing_size: {517 const existing_size = existing_size: {
518 const zo = elf_file.zigObjectPtr() orelse break :existing_size 0;518 const zo = elf_file.zigObjectPtr() orelse break :existing_size 0;
src/link/Elf/relocatable.zig+11-11
...@@ -288,8 +288,8 @@ fn initSections(elf_file: *Elf) !void {...@@ -288,8 +288,8 @@ fn initSections(elf_file: *Elf) !void {
288 } else false;288 } else false;
289 };289 };
290 if (needs_eh_frame) {290 if (needs_eh_frame) {
291 if (elf_file.eh_frame_section_index == null) {291 if (elf_file.section_indexes.eh_frame == null) {
292 elf_file.eh_frame_section_index = elf_file.sectionByName(".eh_frame") orelse292 elf_file.section_indexes.eh_frame = elf_file.sectionByName(".eh_frame") orelse
293 try elf_file.addSection(.{293 try elf_file.addSection(.{
294 .name = try elf_file.insertShString(".eh_frame"),294 .name = try elf_file.insertShString(".eh_frame"),
295 .type = if (elf_file.getTarget().cpu.arch == .x86_64)295 .type = if (elf_file.getTarget().cpu.arch == .x86_64)
...@@ -300,10 +300,10 @@ fn initSections(elf_file: *Elf) !void {...@@ -300,10 +300,10 @@ fn initSections(elf_file: *Elf) !void {
300 .addralign = elf_file.ptrWidthBytes(),300 .addralign = elf_file.ptrWidthBytes(),
301 });301 });
302 }302 }
303 elf_file.eh_frame_rela_section_index = elf_file.sectionByName(".rela.eh_frame") orelse303 elf_file.section_indexes.eh_frame_rela = elf_file.sectionByName(".rela.eh_frame") orelse
304 try elf_file.addRelaShdr(304 try elf_file.addRelaShdr(
305 try elf_file.insertShString(".rela.eh_frame"),305 try elf_file.insertShString(".rela.eh_frame"),
306 elf_file.eh_frame_section_index.?,306 elf_file.section_indexes.eh_frame.?,
307 );307 );
308 }308 }
309309
...@@ -346,7 +346,7 @@ fn updateSectionSizes(elf_file: *Elf) !void {...@@ -346,7 +346,7 @@ fn updateSectionSizes(elf_file: *Elf) !void {
346 for (slice.items(.shdr), 0..) |*shdr, shndx| {346 for (slice.items(.shdr), 0..) |*shdr, shndx| {
347 const atom_list = slice.items(.atom_list)[shndx];347 const atom_list = slice.items(.atom_list)[shndx];
348 if (shdr.sh_type != elf.SHT_RELA) continue;348 if (shdr.sh_type != elf.SHT_RELA) continue;
349 if (@as(u32, @intCast(shndx)) == elf_file.eh_frame_section_index) continue;349 if (@as(u32, @intCast(shndx)) == elf_file.section_indexes.eh_frame) continue;
350 for (atom_list.items) |ref| {350 for (atom_list.items) |ref| {
351 const atom_ptr = elf_file.atom(ref) orelse continue;351 const atom_ptr = elf_file.atom(ref) orelse continue;
352 if (!atom_ptr.alive) continue;352 if (!atom_ptr.alive) continue;
...@@ -357,10 +357,10 @@ fn updateSectionSizes(elf_file: *Elf) !void {...@@ -357,10 +357,10 @@ fn updateSectionSizes(elf_file: *Elf) !void {
357 if (shdr.sh_size == 0) shdr.sh_offset = 0;357 if (shdr.sh_size == 0) shdr.sh_offset = 0;
358 }358 }
359359
360 if (elf_file.eh_frame_section_index) |index| {360 if (elf_file.section_indexes.eh_frame) |index| {
361 slice.items(.shdr)[index].sh_size = try eh_frame.calcEhFrameSize(elf_file);361 slice.items(.shdr)[index].sh_size = try eh_frame.calcEhFrameSize(elf_file);
362 }362 }
363 if (elf_file.eh_frame_rela_section_index) |index| {363 if (elf_file.section_indexes.eh_frame_rela) |index| {
364 const shdr = &slice.items(.shdr)[index];364 const shdr = &slice.items(.shdr)[index];
365 shdr.sh_size = eh_frame.calcEhFrameRelocs(elf_file) * shdr.sh_entsize;365 shdr.sh_size = eh_frame.calcEhFrameRelocs(elf_file) * shdr.sh_entsize;
366 }366 }
...@@ -374,7 +374,7 @@ fn updateComdatGroupsSizes(elf_file: *Elf) void {...@@ -374,7 +374,7 @@ fn updateComdatGroupsSizes(elf_file: *Elf) void {
374 for (elf_file.comdat_group_sections.items) |cg| {374 for (elf_file.comdat_group_sections.items) |cg| {
375 const shdr = &elf_file.sections.items(.shdr)[cg.shndx];375 const shdr = &elf_file.sections.items(.shdr)[cg.shndx];
376 shdr.sh_size = cg.size(elf_file);376 shdr.sh_size = cg.size(elf_file);
377 shdr.sh_link = elf_file.symtab_section_index.?;377 shdr.sh_link = elf_file.section_indexes.symtab.?;
378378
379 const sym = cg.symbol(elf_file);379 const sym = cg.symbol(elf_file);
380 shdr.sh_info = sym.outputSymtabIndex(elf_file) orelse sym.outputShndx(elf_file).?;380 shdr.sh_info = sym.outputSymtabIndex(elf_file) orelse sym.outputShndx(elf_file).?;
...@@ -439,7 +439,7 @@ fn writeSyntheticSections(elf_file: *Elf) !void {...@@ -439,7 +439,7 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
439 for (slice.items(.shdr), slice.items(.atom_list), 0..) |shdr, atom_list, shndx| {439 for (slice.items(.shdr), slice.items(.atom_list), 0..) |shdr, atom_list, shndx| {
440 if (shdr.sh_type != elf.SHT_RELA) continue;440 if (shdr.sh_type != elf.SHT_RELA) continue;
441 if (atom_list.items.len == 0) continue;441 if (atom_list.items.len == 0) continue;
442 if (@as(u32, @intCast(shndx)) == elf_file.eh_frame_section_index) continue;442 if (@as(u32, @intCast(shndx)) == elf_file.section_indexes.eh_frame) continue;
443443
444 const num_relocs = math.cast(usize, @divExact(shdr.sh_size, shdr.sh_entsize)) orelse444 const num_relocs = math.cast(usize, @divExact(shdr.sh_size, shdr.sh_entsize)) orelse
445 return error.Overflow;445 return error.Overflow;
...@@ -471,7 +471,7 @@ fn writeSyntheticSections(elf_file: *Elf) !void {...@@ -471,7 +471,7 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
471 try elf_file.base.file.?.pwriteAll(mem.sliceAsBytes(relocs.items), shdr.sh_offset);471 try elf_file.base.file.?.pwriteAll(mem.sliceAsBytes(relocs.items), shdr.sh_offset);
472 }472 }
473473
474 if (elf_file.eh_frame_section_index) |shndx| {474 if (elf_file.section_indexes.eh_frame) |shndx| {
475 const existing_size = existing_size: {475 const existing_size = existing_size: {
476 const zo = elf_file.zigObjectPtr() orelse break :existing_size 0;476 const zo = elf_file.zigObjectPtr() orelse break :existing_size 0;
477 const sym = zo.symbol(zo.eh_frame_index orelse break :existing_size 0);477 const sym = zo.symbol(zo.eh_frame_index orelse break :existing_size 0);
...@@ -489,7 +489,7 @@ fn writeSyntheticSections(elf_file: *Elf) !void {...@@ -489,7 +489,7 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
489 assert(buffer.items.len == sh_size - existing_size);489 assert(buffer.items.len == sh_size - existing_size);
490 try elf_file.base.file.?.pwriteAll(buffer.items, shdr.sh_offset + existing_size);490 try elf_file.base.file.?.pwriteAll(buffer.items, shdr.sh_offset + existing_size);
491 }491 }
492 if (elf_file.eh_frame_rela_section_index) |shndx| {492 if (elf_file.section_indexes.eh_frame_rela) |shndx| {
493 const shdr = slice.items(.shdr)[shndx];493 const shdr = slice.items(.shdr)[shndx];
494 const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;494 const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
495 var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size);495 var buffer = try std.ArrayList(u8).initCapacity(gpa, sh_size);
src/link/Elf/synthetic_sections.zig+27-27
...@@ -75,18 +75,18 @@ pub const DynamicSection = struct {...@@ -75,18 +75,18 @@ pub const DynamicSection = struct {
75 if (elf_file.sectionByName(".fini") != null) nentries += 1; // FINI75 if (elf_file.sectionByName(".fini") != null) nentries += 1; // FINI
76 if (elf_file.sectionByName(".init_array") != null) nentries += 2; // INIT_ARRAY76 if (elf_file.sectionByName(".init_array") != null) nentries += 2; // INIT_ARRAY
77 if (elf_file.sectionByName(".fini_array") != null) nentries += 2; // FINI_ARRAY77 if (elf_file.sectionByName(".fini_array") != null) nentries += 2; // FINI_ARRAY
78 if (elf_file.rela_dyn_section_index != null) nentries += 3; // RELA78 if (elf_file.section_indexes.rela_dyn != null) nentries += 3; // RELA
79 if (elf_file.rela_plt_section_index != null) nentries += 3; // JMPREL79 if (elf_file.section_indexes.rela_plt != null) nentries += 3; // JMPREL
80 if (elf_file.got_plt_section_index != null) nentries += 1; // PLTGOT80 if (elf_file.section_indexes.got_plt != null) nentries += 1; // PLTGOT
81 nentries += 1; // HASH81 nentries += 1; // HASH
82 if (elf_file.gnu_hash_section_index != null) nentries += 1; // GNU_HASH82 if (elf_file.section_indexes.gnu_hash != null) nentries += 1; // GNU_HASH
83 if (elf_file.has_text_reloc) nentries += 1; // TEXTREL83 if (elf_file.has_text_reloc) nentries += 1; // TEXTREL
84 nentries += 1; // SYMTAB84 nentries += 1; // SYMTAB
85 nentries += 1; // SYMENT85 nentries += 1; // SYMENT
86 nentries += 1; // STRTAB86 nentries += 1; // STRTAB
87 nentries += 1; // STRSZ87 nentries += 1; // STRSZ
88 if (elf_file.versym_section_index != null) nentries += 1; // VERSYM88 if (elf_file.section_indexes.versym != null) nentries += 1; // VERSYM
89 if (elf_file.verneed_section_index != null) nentries += 2; // VERNEED89 if (elf_file.section_indexes.verneed != null) nentries += 2; // VERNEED
90 if (dt.getFlags(elf_file) != null) nentries += 1; // FLAGS90 if (dt.getFlags(elf_file) != null) nentries += 1; // FLAGS
91 if (dt.getFlags1(elf_file) != null) nentries += 1; // FLAGS_191 if (dt.getFlags1(elf_file) != null) nentries += 1; // FLAGS_1
92 if (!elf_file.isEffectivelyDynLib()) nentries += 1; // DEBUG92 if (!elf_file.isEffectivelyDynLib()) nentries += 1; // DEBUG
...@@ -139,7 +139,7 @@ pub const DynamicSection = struct {...@@ -139,7 +139,7 @@ pub const DynamicSection = struct {
139 }139 }
140140
141 // RELA141 // RELA
142 if (elf_file.rela_dyn_section_index) |shndx| {142 if (elf_file.section_indexes.rela_dyn) |shndx| {
143 const shdr = shdrs[shndx];143 const shdr = shdrs[shndx];
144 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_RELA, .d_val = shdr.sh_addr });144 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_RELA, .d_val = shdr.sh_addr });
145 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_RELASZ, .d_val = shdr.sh_size });145 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_RELASZ, .d_val = shdr.sh_size });
...@@ -147,7 +147,7 @@ pub const DynamicSection = struct {...@@ -147,7 +147,7 @@ pub const DynamicSection = struct {
147 }147 }
148148
149 // JMPREL149 // JMPREL
150 if (elf_file.rela_plt_section_index) |shndx| {150 if (elf_file.section_indexes.rela_plt) |shndx| {
151 const shdr = shdrs[shndx];151 const shdr = shdrs[shndx];
152 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_JMPREL, .d_val = shdr.sh_addr });152 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_JMPREL, .d_val = shdr.sh_addr });
153 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_PLTRELSZ, .d_val = shdr.sh_size });153 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_PLTRELSZ, .d_val = shdr.sh_size });
...@@ -155,18 +155,18 @@ pub const DynamicSection = struct {...@@ -155,18 +155,18 @@ pub const DynamicSection = struct {
155 }155 }
156156
157 // PLTGOT157 // PLTGOT
158 if (elf_file.got_plt_section_index) |shndx| {158 if (elf_file.section_indexes.got_plt) |shndx| {
159 const addr = shdrs[shndx].sh_addr;159 const addr = shdrs[shndx].sh_addr;
160 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_PLTGOT, .d_val = addr });160 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_PLTGOT, .d_val = addr });
161 }161 }
162162
163 {163 {
164 assert(elf_file.hash_section_index != null);164 assert(elf_file.section_indexes.hash != null);
165 const addr = shdrs[elf_file.hash_section_index.?].sh_addr;165 const addr = shdrs[elf_file.section_indexes.hash.?].sh_addr;
166 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_HASH, .d_val = addr });166 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_HASH, .d_val = addr });
167 }167 }
168168
169 if (elf_file.gnu_hash_section_index) |shndx| {169 if (elf_file.section_indexes.gnu_hash) |shndx| {
170 const addr = shdrs[shndx].sh_addr;170 const addr = shdrs[shndx].sh_addr;
171 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_GNU_HASH, .d_val = addr });171 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_GNU_HASH, .d_val = addr });
172 }172 }
...@@ -178,28 +178,28 @@ pub const DynamicSection = struct {...@@ -178,28 +178,28 @@ pub const DynamicSection = struct {
178178
179 // SYMTAB + SYMENT179 // SYMTAB + SYMENT
180 {180 {
181 assert(elf_file.dynsymtab_section_index != null);181 assert(elf_file.section_indexes.dynsymtab != null);
182 const shdr = shdrs[elf_file.dynsymtab_section_index.?];182 const shdr = shdrs[elf_file.section_indexes.dynsymtab.?];
183 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_SYMTAB, .d_val = shdr.sh_addr });183 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_SYMTAB, .d_val = shdr.sh_addr });
184 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_SYMENT, .d_val = shdr.sh_entsize });184 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_SYMENT, .d_val = shdr.sh_entsize });
185 }185 }
186186
187 // STRTAB + STRSZ187 // STRTAB + STRSZ
188 {188 {
189 assert(elf_file.dynstrtab_section_index != null);189 assert(elf_file.section_indexes.dynstrtab != null);
190 const shdr = shdrs[elf_file.dynstrtab_section_index.?];190 const shdr = shdrs[elf_file.section_indexes.dynstrtab.?];
191 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_STRTAB, .d_val = shdr.sh_addr });191 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_STRTAB, .d_val = shdr.sh_addr });
192 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_STRSZ, .d_val = shdr.sh_size });192 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_STRSZ, .d_val = shdr.sh_size });
193 }193 }
194194
195 // VERSYM195 // VERSYM
196 if (elf_file.versym_section_index) |shndx| {196 if (elf_file.section_indexes.versym) |shndx| {
197 const addr = shdrs[shndx].sh_addr;197 const addr = shdrs[shndx].sh_addr;
198 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_VERSYM, .d_val = addr });198 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_VERSYM, .d_val = addr });
199 }199 }
200200
201 // VERNEED + VERNEEDNUM201 // VERNEED + VERNEEDNUM
202 if (elf_file.verneed_section_index) |shndx| {202 if (elf_file.section_indexes.verneed) |shndx| {
203 const addr = shdrs[shndx].sh_addr;203 const addr = shdrs[shndx].sh_addr;
204 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_VERNEED, .d_val = addr });204 try writer.writeStruct(elf.Elf64_Dyn{ .d_tag = elf.DT_VERNEED, .d_val = addr });
205 try writer.writeStruct(elf.Elf64_Dyn{205 try writer.writeStruct(elf.Elf64_Dyn{
...@@ -261,7 +261,7 @@ pub const GotSection = struct {...@@ -261,7 +261,7 @@ pub const GotSection = struct {
261261
262 pub fn address(entry: Entry, elf_file: *Elf) i64 {262 pub fn address(entry: Entry, elf_file: *Elf) i64 {
263 const ptr_bytes = elf_file.archPtrWidthBytes();263 const ptr_bytes = elf_file.archPtrWidthBytes();
264 const shdr = &elf_file.sections.items(.shdr)[elf_file.got_section_index.?];264 const shdr = &elf_file.sections.items(.shdr)[elf_file.section_indexes.got.?];
265 return @as(i64, @intCast(shdr.sh_addr)) + entry.cell_index * ptr_bytes;265 return @as(i64, @intCast(shdr.sh_addr)) + entry.cell_index * ptr_bytes;
266 }266 }
267 };267 };
...@@ -599,7 +599,7 @@ pub const GotSection = struct {...@@ -599,7 +599,7 @@ pub const GotSection = struct {
599 .st_name = st_name,599 .st_name = st_name,
600 .st_info = elf.STT_OBJECT,600 .st_info = elf.STT_OBJECT,
601 .st_other = 0,601 .st_other = 0,
602 .st_shndx = @intCast(elf_file.got_section_index.?),602 .st_shndx = @intCast(elf_file.section_indexes.got.?),
603 .st_value = @intCast(st_value),603 .st_value = @intCast(st_value),
604 .st_size = st_size,604 .st_size = st_size,
605 };605 };
...@@ -742,7 +742,7 @@ pub const PltSection = struct {...@@ -742,7 +742,7 @@ pub const PltSection = struct {
742 .st_name = st_name,742 .st_name = st_name,
743 .st_info = elf.STT_FUNC,743 .st_info = elf.STT_FUNC,
744 .st_other = 0,744 .st_other = 0,
745 .st_shndx = @intCast(elf_file.plt_section_index.?),745 .st_shndx = @intCast(elf_file.section_indexes.plt.?),
746 .st_value = @intCast(sym.pltAddress(elf_file)),746 .st_value = @intCast(sym.pltAddress(elf_file)),
747 .st_size = entrySize(cpu_arch),747 .st_size = entrySize(cpu_arch),
748 };748 };
...@@ -784,8 +784,8 @@ pub const PltSection = struct {...@@ -784,8 +784,8 @@ pub const PltSection = struct {
784 const x86_64 = struct {784 const x86_64 = struct {
785 fn write(plt: PltSection, elf_file: *Elf, writer: anytype) !void {785 fn write(plt: PltSection, elf_file: *Elf, writer: anytype) !void {
786 const shdrs = elf_file.sections.items(.shdr);786 const shdrs = elf_file.sections.items(.shdr);
787 const plt_addr = shdrs[elf_file.plt_section_index.?].sh_addr;787 const plt_addr = shdrs[elf_file.section_indexes.plt.?].sh_addr;
788 const got_plt_addr = shdrs[elf_file.got_plt_section_index.?].sh_addr;788 const got_plt_addr = shdrs[elf_file.section_indexes.got_plt.?].sh_addr;
789 var preamble = [_]u8{789 var preamble = [_]u8{
790 0xf3, 0x0f, 0x1e, 0xfa, // endbr64790 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
791 0x41, 0x53, // push r11791 0x41, 0x53, // push r11
...@@ -820,8 +820,8 @@ pub const PltSection = struct {...@@ -820,8 +820,8 @@ pub const PltSection = struct {
820 fn write(plt: PltSection, elf_file: *Elf, writer: anytype) !void {820 fn write(plt: PltSection, elf_file: *Elf, writer: anytype) !void {
821 {821 {
822 const shdrs = elf_file.sections.items(.shdr);822 const shdrs = elf_file.sections.items(.shdr);
823 const plt_addr: i64 = @intCast(shdrs[elf_file.plt_section_index.?].sh_addr);823 const plt_addr: i64 = @intCast(shdrs[elf_file.section_indexes.plt.?].sh_addr);
824 const got_plt_addr: i64 = @intCast(shdrs[elf_file.got_plt_section_index.?].sh_addr);824 const got_plt_addr: i64 = @intCast(shdrs[elf_file.section_indexes.got_plt.?].sh_addr);
825 // TODO: relax if possible825 // TODO: relax if possible
826 // .got.plt[2]826 // .got.plt[2]
827 const pages = try aarch64_util.calcNumberOfPages(plt_addr + 4, got_plt_addr + 16);827 const pages = try aarch64_util.calcNumberOfPages(plt_addr + 4, got_plt_addr + 16);
...@@ -894,7 +894,7 @@ pub const GotPltSection = struct {...@@ -894,7 +894,7 @@ pub const GotPltSection = struct {
894 // [2]: 0x0894 // [2]: 0x0
895 try writer.writeInt(u64, 0x0, .little);895 try writer.writeInt(u64, 0x0, .little);
896 try writer.writeInt(u64, 0x0, .little);896 try writer.writeInt(u64, 0x0, .little);
897 if (elf_file.plt_section_index) |shndx| {897 if (elf_file.section_indexes.plt) |shndx| {
898 const plt_addr = elf_file.sections.items(.shdr)[shndx].sh_addr;898 const plt_addr = elf_file.sections.items(.shdr)[shndx].sh_addr;
899 for (0..elf_file.plt.symbols.items.len) |_| {899 for (0..elf_file.plt.symbols.items.len) |_| {
900 // [N]: .plt900 // [N]: .plt
...@@ -962,7 +962,7 @@ pub const PltGotSection = struct {...@@ -962,7 +962,7 @@ pub const PltGotSection = struct {
962 .st_name = st_name,962 .st_name = st_name,
963 .st_info = elf.STT_FUNC,963 .st_info = elf.STT_FUNC,
964 .st_other = 0,964 .st_other = 0,
965 .st_shndx = @intCast(elf_file.plt_got_section_index.?),965 .st_shndx = @intCast(elf_file.section_indexes.plt_got.?),
966 .st_value = @intCast(sym.pltGotAddress(elf_file)),966 .st_value = @intCast(sym.pltGotAddress(elf_file)),
967 .st_size = 16,967 .st_size = 16,
968 };968 };