@@ -7,8 +7,8 @@ const Section = struct {
7
7
data : std .ArrayList (u8 ),
8
8
relocations : std .ArrayListUnmanaged (Relocation ) = .empty ,
9
9
flags : u64 ,
10
- type : u32 ,
11
- index : u16 = undefined ,
10
+ type : std . elf . SHT ,
11
+ index : std.elf.SHN = undefined ,
12
12
};
13
13
14
14
const Symbol = struct {
@@ -86,7 +86,7 @@ pub fn getSection(elf: *Elf, section_kind: Object.Section) !*std.ArrayList(u8) {
86
86
const section = try elf .arena .allocator ().create (Section );
87
87
section .* = .{
88
88
.data = std .ArrayList (u8 ).init (elf .arena .child_allocator ),
89
- .type = std . elf . SHT_PROGBITS ,
89
+ .type = .PROGBITS ,
90
90
.flags = switch (section_kind ) {
91
91
.func , .custom = > std .elf .SHF_ALLOC + std .elf .SHF_EXECINSTR ,
92
92
.strings = > std .elf .SHF_ALLOC + std .elf .SHF_MERGE + std .elf .SHF_STRINGS ,
@@ -116,16 +116,16 @@ pub fn declareSymbol(
116
116
const section_name = sectionString (section_kind );
117
117
break :blk elf .sections .get (section_name );
118
118
};
119
- const binding : u8 = switch (linkage ) {
120
- .Internal = > std . elf . STB_LOCAL ,
121
- .Strong = > std . elf . STB_GLOBAL ,
122
- .Weak = > std . elf . STB_WEAK ,
119
+ const binding : std.elf.STB = switch (linkage ) {
120
+ .Internal = > .LOCAL ,
121
+ .Strong = > .GLOBAL ,
122
+ .Weak = > .WEAK ,
123
123
.LinkOnce = > unreachable ,
124
124
};
125
- const sym_type : u8 = switch (@"type" ) {
126
- .func = > std . elf . STT_FUNC ,
127
- .variable = > std . elf . STT_OBJECT ,
128
- .external = > std . elf . STT_NOTYPE ,
125
+ const sym_type : std.elf.STT = switch (@"type" ) {
126
+ .func = > .FUNC ,
127
+ .variable = > .OBJECT ,
128
+ .external = > .NOTYPE ,
129
129
};
130
130
const name = if (maybe_name ) | some | some else blk : {
131
131
defer elf .unnamed_symbol_mangle += 1 ;
@@ -174,29 +174,29 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
174
174
var buf_writer = std .io .bufferedWriter (file .writer ());
175
175
const w = buf_writer .writer ();
176
176
177
- var num_sections : std.elf.Elf64_Half = additional_sections ;
178
- var relocations_len : std.elf.Elf64_Off = 0 ;
179
- var sections_len : std.elf.Elf64_Off = 0 ;
177
+ var num_sections : std.elf.Half = additional_sections ;
178
+ var relocations_len : std.elf.elf64.Off = 0 ;
179
+ var sections_len : std.elf.elf64.Off = 0 ;
180
180
{
181
181
var it = elf .sections .valueIterator ();
182
182
while (it .next ()) | sect | {
183
183
sections_len += sect .* .data .items .len ;
184
- relocations_len += sect .* .relocations .items .len * @sizeOf (std .elf .Elf64_Rela );
185
- sect .* .index = num_sections ;
184
+ relocations_len += sect .* .relocations .items .len * @sizeOf (std .elf .elf64 . Rela );
185
+ sect .* .index = @enumFromInt ( num_sections ) ;
186
186
num_sections += 1 ;
187
187
num_sections += @intFromBool (sect .* .relocations .items .len != 0 );
188
188
}
189
189
}
190
- const symtab_len = (elf .local_symbols .count () + elf .global_symbols .count () + 1 ) * @sizeOf (std .elf .Elf64_Sym );
190
+ const symtab_len = (elf .local_symbols .count () + elf .global_symbols .count () + 1 ) * @sizeOf (std .elf .elf64 . Sym );
191
191
192
- const symtab_offset = @sizeOf (std .elf .Elf64_Ehdr ) + sections_len ;
192
+ const symtab_offset = @sizeOf (std .elf .elf64 . Ehdr ) + sections_len ;
193
193
const symtab_offset_aligned = std .mem .alignForward (u64 , symtab_offset , 8 );
194
194
const rela_offset = symtab_offset_aligned + symtab_len ;
195
195
const strtab_offset = rela_offset + relocations_len ;
196
196
const sh_offset = strtab_offset + elf .strtab_len ;
197
197
const sh_offset_aligned = std .mem .alignForward (u64 , sh_offset , 16 );
198
198
199
- const elf_header = std.elf.Elf64_Ehdr {
199
+ const elf_header = std.elf.elf64.Ehdr {
200
200
.e_ident = .{ 0x7F , 'E' , 'L' , 'F' , 2 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
201
201
.e_type = std .elf .ET .REL , // we only produce relocatables
202
202
.e_machine = elf .obj .target .toElfMachine (),
@@ -205,10 +205,10 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
205
205
.e_phoff = 0 , // no program header
206
206
.e_shoff = sh_offset_aligned , // section headers offset
207
207
.e_flags = 0 , // no flags
208
- .e_ehsize = @sizeOf (std .elf .Elf64_Ehdr ),
208
+ .e_ehsize = @sizeOf (std .elf .elf64 . Ehdr ),
209
209
.e_phentsize = 0 , // no program header
210
210
.e_phnum = 0 , // no program header
211
- .e_shentsize = @sizeOf (std .elf .Elf64_Shdr ),
211
+ .e_shentsize = @sizeOf (std .elf .elf64 . Shdr ),
212
212
.e_shnum = num_sections ,
213
213
.e_shstrndx = strtab_index ,
214
214
};
@@ -227,36 +227,36 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
227
227
// write symbols
228
228
{
229
229
// first symbol must be null
230
- try w .writeStruct (std .mem .zeroes (std .elf .Elf64_Sym ));
230
+ try w .writeStruct (std .mem .zeroes (std .elf .elf64 . Sym ));
231
231
232
232
var sym_index : u16 = 1 ;
233
233
var it = elf .local_symbols .iterator ();
234
234
while (it .next ()) | entry | {
235
235
const sym = entry .value_ptr .* ;
236
- try w .writeStruct (std.elf.Elf64_Sym {
236
+ try w .writeStruct (std.elf.elf64.Sym {
237
237
.st_name = name_offset ,
238
238
.st_info = sym .info ,
239
239
.st_other = 0 ,
240
240
.st_shndx = if (sym .section ) | some | some .index else 0 ,
241
241
.st_value = sym .offset ,
242
242
.st_size = sym .size ,
243
243
});
244
- sym .index = sym_index ;
244
+ sym .index = @enumFromInt ( sym_index ) ;
245
245
sym_index += 1 ;
246
246
name_offset += @intCast (entry .key_ptr .len + 1 ); // +1 for null byte
247
247
}
248
248
it = elf .global_symbols .iterator ();
249
249
while (it .next ()) | entry | {
250
250
const sym = entry .value_ptr .* ;
251
- try w .writeStruct (std.elf.Elf64_Sym {
251
+ try w .writeStruct (std.elf.elf64.Sym {
252
252
.st_name = name_offset ,
253
253
.st_info = sym .info ,
254
254
.st_other = 0 ,
255
255
.st_shndx = if (sym .section ) | some | some .index else 0 ,
256
256
.st_value = sym .offset ,
257
257
.st_size = sym .size ,
258
258
});
259
- sym .index = sym_index ;
259
+ sym .index = @enumFromInt ( sym_index ) ;
260
260
sym_index += 1 ;
261
261
name_offset += @intCast (entry .key_ptr .len + 1 ); // +1 for null byte
262
262
}
@@ -267,7 +267,7 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
267
267
var it = elf .sections .valueIterator ();
268
268
while (it .next ()) | sect | {
269
269
for (sect .* .relocations .items ) | rela | {
270
- try w .writeStruct (std.elf.Elf64_Rela {
270
+ try w .writeStruct (std.elf.elf64.Rela {
271
271
.r_offset = rela .offset ,
272
272
.r_addend = rela .addend ,
273
273
.r_info = (@as (u64 , rela .symbol .index ) << 32 ) | rela .type ,
@@ -295,13 +295,13 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
295
295
// pad to 16 bytes
296
296
try w .writeByteNTimes (0 , @intCast (sh_offset_aligned - sh_offset ));
297
297
// mandatory null header
298
- try w .writeStruct (std .mem .zeroes (std .elf .Elf64_Shdr ));
298
+ try w .writeStruct (std .mem .zeroes (std .elf .elf64 . Shdr ));
299
299
300
300
// write strtab section header
301
301
{
302
- const sect_header = std.elf.Elf64_Shdr {
302
+ const sect_header = std.elf.elf64.Shdr {
303
303
.sh_name = strtab_name ,
304
- .sh_type = std . elf . SHT_STRTAB ,
304
+ .sh_type = .STRTAB ,
305
305
.sh_flags = 0 ,
306
306
.sh_addr = 0 ,
307
307
.sh_offset = strtab_offset ,
@@ -316,31 +316,31 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
316
316
317
317
// write symtab section header
318
318
{
319
- const sect_header = std.elf.Elf64_Shdr {
319
+ const sect_header = std.elf.elf64.Shdr {
320
320
.sh_name = symtab_name ,
321
- .sh_type = std . elf . SHT_SYMTAB ,
321
+ .sh_type = .SYMTAB ,
322
322
.sh_flags = 0 ,
323
323
.sh_addr = 0 ,
324
324
.sh_offset = symtab_offset_aligned ,
325
325
.sh_size = symtab_len ,
326
326
.sh_link = strtab_index ,
327
327
.sh_info = elf .local_symbols .size + 1 ,
328
328
.sh_addralign = 8 ,
329
- .sh_entsize = @sizeOf (std .elf .Elf64_Sym ),
329
+ .sh_entsize = @sizeOf (std .elf .elf64 . Sym ),
330
330
};
331
331
try w .writeStruct (sect_header );
332
332
}
333
333
334
334
// remaining section headers
335
335
{
336
- var sect_offset : u64 = @sizeOf (std .elf .Elf64_Ehdr );
336
+ var sect_offset : u64 = @sizeOf (std .elf .elf64 . Ehdr );
337
337
var rela_sect_offset : u64 = rela_offset ;
338
338
var it = elf .sections .iterator ();
339
339
while (it .next ()) | entry | {
340
340
const sect = entry .value_ptr .* ;
341
341
const rela_count = sect .relocations .items .len ;
342
342
const rela_name_offset : u32 = if (rela_count != 0 ) @truncate (".rela" .len ) else 0 ;
343
- try w .writeStruct (std.elf.Elf64_Shdr {
343
+ try w .writeStruct (std.elf.elf64.Shdr {
344
344
.sh_name = rela_name_offset + name_offset ,
345
345
.sh_type = sect .type ,
346
346
.sh_flags = sect .flags ,
@@ -354,18 +354,18 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void {
354
354
});
355
355
356
356
if (rela_count != 0 ) {
357
- const size = rela_count * @sizeOf (std .elf .Elf64_Rela );
358
- try w .writeStruct (std.elf.Elf64_Shdr {
357
+ const size = rela_count * @sizeOf (std .elf .elf64 . Rela );
358
+ try w .writeStruct (std.elf.elf64.Shdr {
359
359
.sh_name = name_offset ,
360
- .sh_type = std . elf . SHT_RELA ,
360
+ .sh_type = .RELA ,
361
361
.sh_flags = 0 ,
362
362
.sh_addr = 0 ,
363
363
.sh_offset = rela_sect_offset ,
364
- .sh_size = rela_count * @sizeOf ( std . elf . Elf64_Rela ) ,
364
+ .sh_size = size ,
365
365
.sh_link = symtab_index ,
366
- .sh_info = sect .index ,
366
+ .sh_info = @intFromEnum ( sect .index ) ,
367
367
.sh_addralign = 8 ,
368
- .sh_entsize = @sizeOf (std .elf .Elf64_Rela ),
368
+ .sh_entsize = @sizeOf (std .elf .elf64 . Rela ),
369
369
});
370
370
rela_sect_offset += size ;
371
371
}
0 commit comments