authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-28 23:19:35+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 10:03:11+02:00
log5ceac8ebbae5fedc56c74e2b3d4918742089ec45
tree6bc07389c720b74d50e6a595190c4dc4a965df4a
parentef7bbcd80fec680b527795dd85bc294b3bf6cbbd

elf: move initializing and allocating linker-defined symbols into LinkerDefined


3 files changed, 233 insertions(+), 229 deletions(-)

src/link/Elf.zig+16-227
......@@ -174,25 +174,6 @@ shstrtab_section_index: ?u32 = null,
174174strtab_section_index: ?u32 = null,
175175symtab_section_index: ?u32 = null,
176176
177// Linker-defined symbols
178dynamic_index: ?Symbol.Index = null,
179ehdr_start_index: ?Symbol.Index = null,
180init_array_start_index: ?Symbol.Index = null,
181init_array_end_index: ?Symbol.Index = null,
182fini_array_start_index: ?Symbol.Index = null,
183fini_array_end_index: ?Symbol.Index = null,
184preinit_array_start_index: ?Symbol.Index = null,
185preinit_array_end_index: ?Symbol.Index = null,
186got_index: ?Symbol.Index = null,
187plt_index: ?Symbol.Index = null,
188end_index: ?Symbol.Index = null,
189gnu_eh_frame_hdr_index: ?Symbol.Index = null,
190dso_handle_index: ?Symbol.Index = null,
191rela_iplt_start_index: ?Symbol.Index = null,
192rela_iplt_end_index: ?Symbol.Index = null,
193global_pointer_index: ?Symbol.Index = null,
194start_stop_indexes: std.ArrayListUnmanaged(u32) = .{},
195
196177/// An array of symbols parsed across all input files.
197178symbols: std.ArrayListUnmanaged(Symbol) = .{},
198179symbols_extra: std.ArrayListUnmanaged(u32) = .{},
......@@ -484,7 +465,6 @@ pub fn deinit(self: *Elf) void {
484465 self.symbols_extra.deinit(gpa);
485466 self.symbols_free_list.deinit(gpa);
486467 self.resolver.deinit(gpa);
487 self.start_stop_indexes.deinit(gpa);
488468
489469 for (self.thunks.items) |*th| {
490470 th.deinit(gpa);
......@@ -1287,7 +1267,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
12871267 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));
12881268 self.files.set(index, .{ .linker_defined = .{ .index = index } });
12891269 self.linker_defined_index = index;
1290 const object = self.file(index).?.linker_defined;
1270 const object = self.linkerDefinedPtr().?;
12911271 try object.init(gpa);
12921272 }
12931273
......@@ -1327,7 +1307,9 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
13271307 try self.finalizeMergeSections();
13281308 try self.initOutputSections();
13291309 try self.initMergeSections();
1330 try self.addLinkerDefinedSymbols();
1310 if (self.linkerDefinedPtr()) |obj| {
1311 try obj.initSymbols(self);
1312 }
13311313 self.claimUnresolved();
13321314
13331315 // Scan and create missing synthetic entries such as GOT indirection.
......@@ -1353,7 +1335,9 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
13531335 try self.sortPhdrs();
13541336 try self.allocateNonAllocSections();
13551337 self.allocateSpecialPhdrs();
1356 self.allocateLinkerDefinedSymbols();
1338 if (self.linkerDefinedPtr()) |obj| {
1339 obj.allocateSymbols(self);
1340 }
13571341
13581342 // Dump the state for easy debugging.
13591343 // State can be dumped via `--debug-log link_state`.
......@@ -3063,205 +3047,6 @@ pub fn deleteExport(
30633047 return self.zigObjectPtr().?.deleteExport(self, exported, name);
30643048}
30653049
3066fn addLinkerDefinedSymbols(self: *Elf) !void {
3067 const comp = self.base.comp;
3068 const gpa = comp.gpa;
3069
3070 const linker_defined_index = self.linker_defined_index orelse return;
3071 const linker_defined = self.file(linker_defined_index).?.linker_defined;
3072 self.dynamic_index = try linker_defined.addGlobal("_DYNAMIC", self);
3073 self.ehdr_start_index = try linker_defined.addGlobal("__ehdr_start", self);
3074 self.init_array_start_index = try linker_defined.addGlobal("__init_array_start", self);
3075 self.init_array_end_index = try linker_defined.addGlobal("__init_array_end", self);
3076 self.fini_array_start_index = try linker_defined.addGlobal("__fini_array_start", self);
3077 self.fini_array_end_index = try linker_defined.addGlobal("__fini_array_end", self);
3078 self.preinit_array_start_index = try linker_defined.addGlobal("__preinit_array_start", self);
3079 self.preinit_array_end_index = try linker_defined.addGlobal("__preinit_array_end", self);
3080 self.got_index = try linker_defined.addGlobal("_GLOBAL_OFFSET_TABLE_", self);
3081 self.plt_index = try linker_defined.addGlobal("_PROCEDURE_LINKAGE_TABLE_", self);
3082 self.end_index = try linker_defined.addGlobal("_end", self);
3083
3084 if (comp.link_eh_frame_hdr) {
3085 self.gnu_eh_frame_hdr_index = try linker_defined.addGlobal("__GNU_EH_FRAME_HDR", self);
3086 }
3087
3088 if (self.globalByName("__dso_handle")) |index| {
3089 if (self.symbol(index).file(self) == null)
3090 self.dso_handle_index = try linker_defined.addGlobal("__dso_handle", self);
3091 }
3092
3093 self.rela_iplt_start_index = try linker_defined.addGlobal("__rela_iplt_start", self);
3094 self.rela_iplt_end_index = try linker_defined.addGlobal("__rela_iplt_end", self);
3095
3096 for (self.shdrs.items) |shdr| {
3097 if (self.getStartStopBasename(shdr)) |name| {
3098 try self.start_stop_indexes.ensureUnusedCapacity(gpa, 2);
3099
3100 const start = try std.fmt.allocPrintZ(gpa, "__start_{s}", .{name});
3101 defer gpa.free(start);
3102 const stop = try std.fmt.allocPrintZ(gpa, "__stop_{s}", .{name});
3103 defer gpa.free(stop);
3104
3105 self.start_stop_indexes.appendAssumeCapacity(try linker_defined.addGlobal(start, self));
3106 self.start_stop_indexes.appendAssumeCapacity(try linker_defined.addGlobal(stop, self));
3107 }
3108 }
3109
3110 if (self.getTarget().cpu.arch.isRISCV() and self.isEffectivelyDynLib()) {
3111 self.global_pointer_index = try linker_defined.addGlobal("__global_pointer$", self);
3112 }
3113
3114 linker_defined.resolveSymbols(self);
3115}
3116
3117fn allocateLinkerDefinedSymbols(self: *Elf) void {
3118 const comp = self.base.comp;
3119 const link_mode = comp.config.link_mode;
3120
3121 // _DYNAMIC
3122 if (self.dynamic_section_index) |shndx| {
3123 const shdr = &self.shdrs.items[shndx];
3124 const symbol_ptr = self.symbol(self.dynamic_index.?);
3125 symbol_ptr.value = @intCast(shdr.sh_addr);
3126 symbol_ptr.output_section_index = shndx;
3127 }
3128
3129 // __ehdr_start
3130 {
3131 const symbol_ptr = self.symbol(self.ehdr_start_index.?);
3132 symbol_ptr.value = @intCast(self.image_base);
3133 symbol_ptr.output_section_index = 1;
3134 }
3135
3136 // __init_array_start, __init_array_end
3137 if (self.sectionByName(".init_array")) |shndx| {
3138 const start_sym = self.symbol(self.init_array_start_index.?);
3139 const end_sym = self.symbol(self.init_array_end_index.?);
3140 const shdr = &self.shdrs.items[shndx];
3141 start_sym.output_section_index = shndx;
3142 start_sym.value = @intCast(shdr.sh_addr);
3143 end_sym.output_section_index = shndx;
3144 end_sym.value = @intCast(shdr.sh_addr + shdr.sh_size);
3145 }
3146
3147 // __fini_array_start, __fini_array_end
3148 if (self.sectionByName(".fini_array")) |shndx| {
3149 const start_sym = self.symbol(self.fini_array_start_index.?);
3150 const end_sym = self.symbol(self.fini_array_end_index.?);
3151 const shdr = &self.shdrs.items[shndx];
3152 start_sym.output_section_index = shndx;
3153 start_sym.value = @intCast(shdr.sh_addr);
3154 end_sym.output_section_index = shndx;
3155 end_sym.value = @intCast(shdr.sh_addr + shdr.sh_size);
3156 }
3157
3158 // __preinit_array_start, __preinit_array_end
3159 if (self.sectionByName(".preinit_array")) |shndx| {
3160 const start_sym = self.symbol(self.preinit_array_start_index.?);
3161 const end_sym = self.symbol(self.preinit_array_end_index.?);
3162 const shdr = &self.shdrs.items[shndx];
3163 start_sym.output_section_index = shndx;
3164 start_sym.value = @intCast(shdr.sh_addr);
3165 end_sym.output_section_index = shndx;
3166 end_sym.value = @intCast(shdr.sh_addr + shdr.sh_size);
3167 }
3168
3169 // _GLOBAL_OFFSET_TABLE_
3170 if (self.getTarget().cpu.arch == .x86_64) {
3171 if (self.got_plt_section_index) |shndx| {
3172 const shdr = self.shdrs.items[shndx];
3173 const sym = self.symbol(self.got_index.?);
3174 sym.value = @intCast(shdr.sh_addr);
3175 sym.output_section_index = shndx;
3176 }
3177 } else {
3178 if (self.got_section_index) |shndx| {
3179 const shdr = self.shdrs.items[shndx];
3180 const sym = self.symbol(self.got_index.?);
3181 sym.value = @intCast(shdr.sh_addr);
3182 sym.output_section_index = shndx;
3183 }
3184 }
3185
3186 // _PROCEDURE_LINKAGE_TABLE_
3187 if (self.plt_section_index) |shndx| {
3188 const shdr = &self.shdrs.items[shndx];
3189 const symbol_ptr = self.symbol(self.plt_index.?);
3190 symbol_ptr.value = @intCast(shdr.sh_addr);
3191 symbol_ptr.output_section_index = shndx;
3192 }
3193
3194 // __dso_handle
3195 if (self.dso_handle_index) |index| {
3196 const shdr = &self.shdrs.items[1];
3197 const symbol_ptr = self.symbol(index);
3198 symbol_ptr.value = @intCast(shdr.sh_addr);
3199 symbol_ptr.output_section_index = 0;
3200 }
3201
3202 // __GNU_EH_FRAME_HDR
3203 if (self.eh_frame_hdr_section_index) |shndx| {
3204 const shdr = &self.shdrs.items[shndx];
3205 const symbol_ptr = self.symbol(self.gnu_eh_frame_hdr_index.?);
3206 symbol_ptr.value = @intCast(shdr.sh_addr);
3207 symbol_ptr.output_section_index = shndx;
3208 }
3209
3210 // __rela_iplt_start, __rela_iplt_end
3211 if (self.rela_dyn_section_index) |shndx| blk: {
3212 if (link_mode != .static or comp.config.pie) break :blk;
3213 const shdr = &self.shdrs.items[shndx];
3214 const end_addr = shdr.sh_addr + shdr.sh_size;
3215 const start_addr = end_addr - self.calcNumIRelativeRelocs() * @sizeOf(elf.Elf64_Rela);
3216 const start_sym = self.symbol(self.rela_iplt_start_index.?);
3217 const end_sym = self.symbol(self.rela_iplt_end_index.?);
3218 start_sym.value = @intCast(start_addr);
3219 start_sym.output_section_index = shndx;
3220 end_sym.value = @intCast(end_addr);
3221 end_sym.output_section_index = shndx;
3222 }
3223
3224 // _end
3225 {
3226 const end_symbol = self.symbol(self.end_index.?);
3227 for (self.shdrs.items, 0..) |shdr, shndx| {
3228 if (shdr.sh_flags & elf.SHF_ALLOC != 0) {
3229 end_symbol.value = @intCast(shdr.sh_addr + shdr.sh_size);
3230 end_symbol.output_section_index = @intCast(shndx);
3231 }
3232 }
3233 }
3234
3235 // __start_*, __stop_*
3236 {
3237 var index: usize = 0;
3238 while (index < self.start_stop_indexes.items.len) : (index += 2) {
3239 const start = self.symbol(self.start_stop_indexes.items[index]);
3240 const name = start.name(self);
3241 const stop = self.symbol(self.start_stop_indexes.items[index + 1]);
3242 const shndx = self.sectionByName(name["__start_".len..]).?;
3243 const shdr = &self.shdrs.items[shndx];
3244 start.value = @intCast(shdr.sh_addr);
3245 start.output_section_index = shndx;
3246 stop.value = @intCast(shdr.sh_addr + shdr.sh_size);
3247 stop.output_section_index = shndx;
3248 }
3249 }
3250
3251 // __global_pointer$
3252 if (self.global_pointer_index) |index| {
3253 const sym = self.symbol(index);
3254 if (self.sectionByName(".sdata")) |shndx| {
3255 const shdr = self.shdrs.items[shndx];
3256 sym.value = @intCast(shdr.sh_addr + 0x800);
3257 sym.output_section_index = shndx;
3258 } else {
3259 sym.value = 0;
3260 sym.output_section_index = 0;
3261 }
3262 }
3263}
3264
32653050fn checkDuplicates(self: *Elf) !void {
32663051 const gpa = self.base.comp.gpa;
32673052
......@@ -4964,9 +4749,8 @@ pub fn writeSymtab(self: *Elf) !void {
49644749 file_ptr.writeSymtab(self);
49654750 }
49664751
4967 if (self.linker_defined_index) |index| {
4968 const file_ptr = self.file(index).?;
4969 file_ptr.writeSymtab(self);
4752 if (self.linkerDefinedPtr()) |obj| {
4753 obj.asFile().writeSymtab(self);
49704754 }
49714755
49724756 if (self.zig_got_section_index) |_| {
......@@ -5529,7 +5313,7 @@ fn sortRelaDyn(self: *Elf) void {
55295313 mem.sort(elf.Elf64_Rela, self.rela_dyn.items, self, Sort.lessThan);
55305314}
55315315
5532fn calcNumIRelativeRelocs(self: *Elf) usize {
5316pub fn calcNumIRelativeRelocs(self: *Elf) usize {
55335317 var count: usize = self.num_ifunc_dynrelocs;
55345318
55355319 for (self.got.entries.items) |entry| {
......@@ -5551,7 +5335,7 @@ pub fn isCIdentifier(name: []const u8) bool {
55515335 return true;
55525336}
55535337
5554fn getStartStopBasename(self: *Elf, shdr: elf.Elf64_Shdr) ?[]const u8 {
5338pub fn getStartStopBasename(self: *Elf, shdr: elf.Elf64_Shdr) ?[]const u8 {
55555339 const name = self.getShString(shdr.sh_name);
55565340 if (shdr.sh_flags & elf.SHF_ALLOC != 0 and name.len > 0) {
55575341 if (isCIdentifier(name)) return name;
......@@ -5709,6 +5493,11 @@ pub fn zigObjectPtr(self: *Elf) ?*ZigObject {
57095493 return self.file(index).?.zig_object;
57105494}
57115495
5496pub fn linkerDefinedPtr(self: *Elf) ?*LinkerDefined {
5497 const index = self.linker_defined_index orelse return null;
5498 return self.file(index).?.linker_defined;
5499}
5500
57125501pub fn getOrCreateMergeSection(self: *Elf, name: [:0]const u8, flags: u64, @"type": u32) !MergeSection.Index {
57135502 const gpa = self.base.comp.gpa;
57145503 const out_name = name: {
src/link/Elf/LinkerDefined.zig+216-1
......@@ -4,12 +4,31 @@ symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
44strtab: std.ArrayListUnmanaged(u8) = .{},
55symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
66
7dynamic_index: ?Symbol.Index = null,
8ehdr_start_index: ?Symbol.Index = null,
9init_array_start_index: ?Symbol.Index = null,
10init_array_end_index: ?Symbol.Index = null,
11fini_array_start_index: ?Symbol.Index = null,
12fini_array_end_index: ?Symbol.Index = null,
13preinit_array_start_index: ?Symbol.Index = null,
14preinit_array_end_index: ?Symbol.Index = null,
15got_index: ?Symbol.Index = null,
16plt_index: ?Symbol.Index = null,
17end_index: ?Symbol.Index = null,
18gnu_eh_frame_hdr_index: ?Symbol.Index = null,
19dso_handle_index: ?Symbol.Index = null,
20rela_iplt_start_index: ?Symbol.Index = null,
21rela_iplt_end_index: ?Symbol.Index = null,
22global_pointer_index: ?Symbol.Index = null,
23start_stop_indexes: std.ArrayListUnmanaged(u32) = .{},
24
725output_symtab_ctx: Elf.SymtabCtx = .{},
826
927pub fn deinit(self: *LinkerDefined, allocator: Allocator) void {
1028 self.symtab.deinit(allocator);
1129 self.strtab.deinit(allocator);
1230 self.symbols.deinit(allocator);
31 self.start_stop_indexes.deinit(allocator);
1332}
1433
1534pub fn init(self: *LinkerDefined, allocator: Allocator) !void {
......@@ -17,7 +36,55 @@ pub fn init(self: *LinkerDefined, allocator: Allocator) !void {
1736 try self.strtab.append(allocator, 0);
1837}
1938
20pub fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32 {
39pub fn initSymbols(self: *LinkerDefined, elf_file: *Elf) !void {
40 const gpa = elf_file.base.comp.gpa;
41
42 self.dynamic_index = try self.addGlobal("_DYNAMIC", elf_file);
43 self.ehdr_start_index = try self.addGlobal("__ehdr_start", elf_file);
44 self.init_array_start_index = try self.addGlobal("__init_array_start", elf_file);
45 self.init_array_end_index = try self.addGlobal("__init_array_end", elf_file);
46 self.fini_array_start_index = try self.addGlobal("__fini_array_start", elf_file);
47 self.fini_array_end_index = try self.addGlobal("__fini_array_end", elf_file);
48 self.preinit_array_start_index = try self.addGlobal("__preinit_array_start", elf_file);
49 self.preinit_array_end_index = try self.addGlobal("__preinit_array_end", elf_file);
50 self.got_index = try self.addGlobal("_GLOBAL_OFFSET_TABLE_", elf_file);
51 self.plt_index = try self.addGlobal("_PROCEDURE_LINKAGE_TABLE_", elf_file);
52 self.end_index = try self.addGlobal("_end", elf_file);
53
54 if (elf_file.base.comp.link_eh_frame_hdr) {
55 self.gnu_eh_frame_hdr_index = try self.addGlobal("__GNU_EH_FRAME_HDR", elf_file);
56 }
57
58 if (elf_file.globalByName("__dso_handle")) |index| {
59 if (elf_file.symbol(index).file(elf_file) == null)
60 self.dso_handle_index = try self.addGlobal("__dso_handle", elf_file);
61 }
62
63 self.rela_iplt_start_index = try self.addGlobal("__rela_iplt_start", elf_file);
64 self.rela_iplt_end_index = try self.addGlobal("__rela_iplt_end", elf_file);
65
66 for (elf_file.shdrs.items) |shdr| {
67 if (elf_file.getStartStopBasename(shdr)) |name| {
68 try self.start_stop_indexes.ensureUnusedCapacity(gpa, 2);
69
70 const start = try std.fmt.allocPrintZ(gpa, "__start_{s}", .{name});
71 defer gpa.free(start);
72 const stop = try std.fmt.allocPrintZ(gpa, "__stop_{s}", .{name});
73 defer gpa.free(stop);
74
75 self.start_stop_indexes.appendAssumeCapacity(try self.addGlobal(start, elf_file));
76 self.start_stop_indexes.appendAssumeCapacity(try self.addGlobal(stop, elf_file));
77 }
78 }
79
80 if (elf_file.getTarget().cpu.arch.isRISCV() and elf_file.isEffectivelyDynLib()) {
81 self.global_pointer_index = try self.addGlobal("__global_pointer$", elf_file);
82 }
83
84 self.resolveSymbols(elf_file);
85}
86
87fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32 {
2188 const comp = elf_file.base.comp;
2289 const gpa = comp.gpa;
2390 try self.symtab.ensureUnusedCapacity(gpa, 1);
......@@ -55,6 +122,154 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
55122 }
56123}
57124
125pub fn allocateSymbols(self: *LinkerDefined, elf_file: *Elf) void {
126 const comp = elf_file.base.comp;
127 const link_mode = comp.config.link_mode;
128
129 // _DYNAMIC
130 if (elf_file.dynamic_section_index) |shndx| {
131 const shdr = &elf_file.shdrs.items[shndx];
132 const symbol_ptr = elf_file.symbol(self.dynamic_index.?);
133 symbol_ptr.value = @intCast(shdr.sh_addr);
134 symbol_ptr.output_section_index = shndx;
135 }
136
137 // __ehdr_start
138 {
139 const symbol_ptr = elf_file.symbol(self.ehdr_start_index.?);
140 symbol_ptr.value = @intCast(elf_file.image_base);
141 symbol_ptr.output_section_index = 1;
142 }
143
144 // __init_array_start, __init_array_end
145 if (elf_file.sectionByName(".init_array")) |shndx| {
146 const start_sym = elf_file.symbol(self.init_array_start_index.?);
147 const end_sym = elf_file.symbol(self.init_array_end_index.?);
148 const shdr = &elf_file.shdrs.items[shndx];
149 start_sym.output_section_index = shndx;
150 start_sym.value = @intCast(shdr.sh_addr);
151 end_sym.output_section_index = shndx;
152 end_sym.value = @intCast(shdr.sh_addr + shdr.sh_size);
153 }
154
155 // __fini_array_start, __fini_array_end
156 if (elf_file.sectionByName(".fini_array")) |shndx| {
157 const start_sym = elf_file.symbol(self.fini_array_start_index.?);
158 const end_sym = elf_file.symbol(self.fini_array_end_index.?);
159 const shdr = &elf_file.shdrs.items[shndx];
160 start_sym.output_section_index = shndx;
161 start_sym.value = @intCast(shdr.sh_addr);
162 end_sym.output_section_index = shndx;
163 end_sym.value = @intCast(shdr.sh_addr + shdr.sh_size);
164 }
165
166 // __preinit_array_start, __preinit_array_end
167 if (elf_file.sectionByName(".preinit_array")) |shndx| {
168 const start_sym = elf_file.symbol(self.preinit_array_start_index.?);
169 const end_sym = elf_file.symbol(self.preinit_array_end_index.?);
170 const shdr = &elf_file.shdrs.items[shndx];
171 start_sym.output_section_index = shndx;
172 start_sym.value = @intCast(shdr.sh_addr);
173 end_sym.output_section_index = shndx;
174 end_sym.value = @intCast(shdr.sh_addr + shdr.sh_size);
175 }
176
177 // _GLOBAL_OFFSET_TABLE_
178 if (elf_file.getTarget().cpu.arch == .x86_64) {
179 if (elf_file.got_plt_section_index) |shndx| {
180 const shdr = elf_file.shdrs.items[shndx];
181 const sym = elf_file.symbol(self.got_index.?);
182 sym.value = @intCast(shdr.sh_addr);
183 sym.output_section_index = shndx;
184 }
185 } else {
186 if (elf_file.got_section_index) |shndx| {
187 const shdr = elf_file.shdrs.items[shndx];
188 const sym = elf_file.symbol(self.got_index.?);
189 sym.value = @intCast(shdr.sh_addr);
190 sym.output_section_index = shndx;
191 }
192 }
193
194 // _PROCEDURE_LINKAGE_TABLE_
195 if (elf_file.plt_section_index) |shndx| {
196 const shdr = &elf_file.shdrs.items[shndx];
197 const symbol_ptr = elf_file.symbol(self.plt_index.?);
198 symbol_ptr.value = @intCast(shdr.sh_addr);
199 symbol_ptr.output_section_index = shndx;
200 }
201
202 // __dso_handle
203 if (self.dso_handle_index) |index| {
204 const shdr = &elf_file.shdrs.items[1];
205 const symbol_ptr = elf_file.symbol(index);
206 symbol_ptr.value = @intCast(shdr.sh_addr);
207 symbol_ptr.output_section_index = 0;
208 }
209
210 // __GNU_EH_FRAME_HDR
211 if (elf_file.eh_frame_hdr_section_index) |shndx| {
212 const shdr = &elf_file.shdrs.items[shndx];
213 const symbol_ptr = elf_file.symbol(self.gnu_eh_frame_hdr_index.?);
214 symbol_ptr.value = @intCast(shdr.sh_addr);
215 symbol_ptr.output_section_index = shndx;
216 }
217
218 // __rela_iplt_start, __rela_iplt_end
219 if (elf_file.rela_dyn_section_index) |shndx| blk: {
220 if (link_mode != .static or comp.config.pie) break :blk;
221 const shdr = &elf_file.shdrs.items[shndx];
222 const end_addr = shdr.sh_addr + shdr.sh_size;
223 const start_addr = end_addr - elf_file.calcNumIRelativeRelocs() * @sizeOf(elf.Elf64_Rela);
224 const start_sym = elf_file.symbol(self.rela_iplt_start_index.?);
225 const end_sym = elf_file.symbol(self.rela_iplt_end_index.?);
226 start_sym.value = @intCast(start_addr);
227 start_sym.output_section_index = shndx;
228 end_sym.value = @intCast(end_addr);
229 end_sym.output_section_index = shndx;
230 }
231
232 // _end
233 {
234 const end_symbol = elf_file.symbol(self.end_index.?);
235 for (elf_file.shdrs.items, 0..) |shdr, shndx| {
236 if (shdr.sh_flags & elf.SHF_ALLOC != 0) {
237 end_symbol.value = @intCast(shdr.sh_addr + shdr.sh_size);
238 end_symbol.output_section_index = @intCast(shndx);
239 }
240 }
241 }
242
243 // __start_*, __stop_*
244 {
245 var index: usize = 0;
246 while (index < self.start_stop_indexes.items.len) : (index += 2) {
247 const start = elf_file.symbol(self.start_stop_indexes.items[index]);
248 const name = start.name(elf_file);
249 const stop = elf_file.symbol(self.start_stop_indexes.items[index + 1]);
250 const shndx = elf_file.sectionByName(name["__start_".len..]).?;
251 const shdr = &elf_file.shdrs.items[shndx];
252 start.value = @intCast(shdr.sh_addr);
253 start.output_section_index = shndx;
254 stop.value = @intCast(shdr.sh_addr + shdr.sh_size);
255 stop.output_section_index = shndx;
256 }
257 }
258
259 // __global_pointer$
260 if (self.global_pointer_index) |index| {
261 const sym = elf_file.symbol(index);
262 if (elf_file.sectionByName(".sdata")) |shndx| {
263 const shdr = elf_file.shdrs.items[shndx];
264 sym.value = @intCast(shdr.sh_addr + 0x800);
265 sym.output_section_index = shndx;
266 } else {
267 sym.value = 0;
268 sym.output_section_index = 0;
269 }
270 }
271}
272
58273pub fn globals(self: LinkerDefined) []const Symbol.Index {
59274 return self.symbols.items;
60275}
src/link/Elf/synthetic_sections.zig+1-1
......@@ -1075,7 +1075,7 @@ pub const GotPltSection = struct {
10751075 _ = got_plt;
10761076 {
10771077 // [0]: _DYNAMIC
1078 const symbol = elf_file.symbol(elf_file.dynamic_index.?);
1078 const symbol = elf_file.symbol(elf_file.linkerDefinedPtr().?.dynamic_index.?);
10791079 try writer.writeInt(u64, @intCast(symbol.address(.{}, elf_file)), .little);
10801080 }
10811081 // [1]: 0x0