@@ -4,6 +4,7 @@ epilogue: []const Instruction,
4
4
literals : []const u32 ,
5
5
nav_relocs : []const Reloc.Nav ,
6
6
uav_relocs : []const Reloc.Uav ,
7
+ lazy_relocs : []const Reloc.Lazy ,
7
8
global_relocs : []const Reloc.Global ,
8
9
literal_relocs : []const Reloc.Literal ,
9
10
@@ -21,8 +22,13 @@ pub const Reloc = struct {
21
22
reloc : Reloc ,
22
23
};
23
24
25
+ pub const Lazy = struct {
26
+ symbol : link.File.LazySymbol ,
27
+ reloc : Reloc ,
28
+ };
29
+
24
30
pub const Global = struct {
25
- global : [* :0 ]const u8 ,
31
+ name : [* :0 ]const u8 ,
26
32
reloc : Reloc ,
27
33
};
28
34
@@ -38,6 +44,7 @@ pub fn deinit(mir: *Mir, gpa: std.mem.Allocator) void {
38
44
gpa .free (mir .literals );
39
45
gpa .free (mir .nav_relocs );
40
46
gpa .free (mir .uav_relocs );
47
+ gpa .free (mir .lazy_relocs );
41
48
gpa .free (mir .global_relocs );
42
49
gpa .free (mir .literal_relocs );
43
50
mir .* = undefined ;
@@ -119,16 +126,37 @@ pub fn emit(
119
126
body_end - Instruction .size * (1 + uav_reloc .reloc .label ),
120
127
uav_reloc .reloc .addend ,
121
128
);
129
+ for (mir .lazy_relocs ) | lazy_reloc | try emitReloc (
130
+ lf ,
131
+ zcu ,
132
+ func .owner_nav ,
133
+ if (lf .cast (.elf )) | ef |
134
+ ef .zigObjectPtr ().? .getOrCreateMetadataForLazySymbol (ef , pt , lazy_reloc .symbol ) catch | err |
135
+ return zcu .codegenFail (func .owner_nav , "{s} creating lazy symbol" , .{@errorName (err )})
136
+ else if (lf .cast (.macho )) | mf |
137
+ mf .getZigObject ().? .getOrCreateMetadataForLazySymbol (mf , pt , lazy_reloc .symbol ) catch | err |
138
+ return zcu .codegenFail (func .owner_nav , "{s} creating lazy symbol" , .{@errorName (err )})
139
+ else if (lf .cast (.coff )) | cf |
140
+ if (cf .getOrCreateAtomForLazySymbol (pt , lazy_reloc .symbol )) | atom |
141
+ cf .getAtom (atom ).getSymbolIndex ().?
142
+ else | err |
143
+ return zcu .codegenFail (func .owner_nav , "{s} creating lazy symbol" , .{@errorName (err )})
144
+ else
145
+ return zcu .codegenFail (func .owner_nav , "external symbols unimplemented for {s}" , .{@tagName (lf .tag )}),
146
+ mir .body [lazy_reloc .reloc .label ],
147
+ body_end - Instruction .size * (1 + lazy_reloc .reloc .label ),
148
+ lazy_reloc .reloc .addend ,
149
+ );
122
150
for (mir .global_relocs ) | global_reloc | try emitReloc (
123
151
lf ,
124
152
zcu ,
125
153
func .owner_nav ,
126
154
if (lf .cast (.elf )) | ef |
127
- try ef .getGlobalSymbol (std .mem .span (global_reloc .global ), null )
155
+ try ef .getGlobalSymbol (std .mem .span (global_reloc .name ), null )
128
156
else if (lf .cast (.macho )) | mf |
129
- try mf .getGlobalSymbol (std .mem .span (global_reloc .global ), null )
157
+ try mf .getGlobalSymbol (std .mem .span (global_reloc .name ), null )
130
158
else if (lf .cast (.coff )) | cf |
131
- try cf .getGlobalSymbol (std .mem .span (global_reloc .global ), "compiler_rt" )
159
+ try cf .getGlobalSymbol (std .mem .span (global_reloc .name ), "compiler_rt" )
132
160
else
133
161
return zcu .codegenFail (func .owner_nav , "external symbols unimplemented for {s}" , .{@tagName (lf .tag )}),
134
162
mir .body [global_reloc .reloc .label ],
@@ -172,35 +200,6 @@ fn emitReloc(
172
200
const gpa = zcu .gpa ;
173
201
switch (instruction .decode ()) {
174
202
else = > unreachable ,
175
- .branch_exception_generating_system = > | decoded | if (lf .cast (.elf )) | ef | {
176
- const zo = ef .zigObjectPtr ().? ;
177
- const atom = zo .symbol (try zo .getOrCreateMetadataForNav (zcu , owner_nav )).atom (ef ).? ;
178
- const r_type : std.elf.R_AARCH64 = switch (decoded .decode ().unconditional_branch_immediate .group .op ) {
179
- .b = > .JUMP26 ,
180
- .bl = > .CALL26 ,
181
- };
182
- try atom .addReloc (gpa , .{
183
- .r_offset = offset ,
184
- .r_info = @as (u64 , sym_index ) << 32 | @intFromEnum (r_type ),
185
- .r_addend = @bitCast (addend ),
186
- }, zo );
187
- } else if (lf .cast (.macho )) | mf | {
188
- const zo = mf .getZigObject ().? ;
189
- const atom = zo .symbols .items [try zo .getOrCreateMetadataForNav (mf , owner_nav )].getAtom (mf ).? ;
190
- try atom .addReloc (mf , .{
191
- .tag = .@"extern" ,
192
- .offset = offset ,
193
- .target = sym_index ,
194
- .addend = @bitCast (addend ),
195
- .type = .branch ,
196
- .meta = .{
197
- .pcrel = true ,
198
- .has_subtractor = false ,
199
- .length = 2 ,
200
- .symbolnum = @intCast (sym_index ),
201
- },
202
- });
203
- },
204
203
.data_processing_immediate = > | decoded | if (lf .cast (.elf )) | ef | {
205
204
const zo = ef .zigObjectPtr ().? ;
206
205
const atom = zo .symbol (try zo .getOrCreateMetadataForNav (zcu , owner_nav )).atom (ef ).? ;
@@ -259,6 +258,80 @@ fn emitReloc(
259
258
},
260
259
}
261
260
},
261
+ .branch_exception_generating_system = > | decoded | if (lf .cast (.elf )) | ef | {
262
+ const zo = ef .zigObjectPtr ().? ;
263
+ const atom = zo .symbol (try zo .getOrCreateMetadataForNav (zcu , owner_nav )).atom (ef ).? ;
264
+ const r_type : std.elf.R_AARCH64 = switch (decoded .decode ().unconditional_branch_immediate .group .op ) {
265
+ .b = > .JUMP26 ,
266
+ .bl = > .CALL26 ,
267
+ };
268
+ try atom .addReloc (gpa , .{
269
+ .r_offset = offset ,
270
+ .r_info = @as (u64 , sym_index ) << 32 | @intFromEnum (r_type ),
271
+ .r_addend = @bitCast (addend ),
272
+ }, zo );
273
+ } else if (lf .cast (.macho )) | mf | {
274
+ const zo = mf .getZigObject ().? ;
275
+ const atom = zo .symbols .items [try zo .getOrCreateMetadataForNav (mf , owner_nav )].getAtom (mf ).? ;
276
+ try atom .addReloc (mf , .{
277
+ .tag = .@"extern" ,
278
+ .offset = offset ,
279
+ .target = sym_index ,
280
+ .addend = @bitCast (addend ),
281
+ .type = .branch ,
282
+ .meta = .{
283
+ .pcrel = true ,
284
+ .has_subtractor = false ,
285
+ .length = 2 ,
286
+ .symbolnum = @intCast (sym_index ),
287
+ },
288
+ });
289
+ },
290
+ .load_store = > | decoded | if (lf .cast (.elf )) | ef | {
291
+ const zo = ef .zigObjectPtr ().? ;
292
+ const atom = zo .symbol (try zo .getOrCreateMetadataForNav (zcu , owner_nav )).atom (ef ).? ;
293
+ const r_type : std.elf.R_AARCH64 = switch (decoded .decode ().register_unsigned_immediate .decode ()) {
294
+ .integer = > | integer | switch (integer .decode ()) {
295
+ .unallocated , .prfm = > unreachable ,
296
+ .strb , .ldrb , .ldrsb = > .LDST8_ABS_LO12_NC ,
297
+ .strh , .ldrh , .ldrsh = > .LDST16_ABS_LO12_NC ,
298
+ .ldrsw = > .LDST32_ABS_LO12_NC ,
299
+ inline .str , .ldr = > | encoded | switch (encoded .sf ) {
300
+ .word = > .LDST32_ABS_LO12_NC ,
301
+ .doubleword = > .LDST64_ABS_LO12_NC ,
302
+ },
303
+ },
304
+ .vector = > | vector | switch (vector .group .opc1 .decode (vector .group .size )) {
305
+ .byte = > .LDST8_ABS_LO12_NC ,
306
+ .half = > .LDST16_ABS_LO12_NC ,
307
+ .single = > .LDST32_ABS_LO12_NC ,
308
+ .double = > .LDST64_ABS_LO12_NC ,
309
+ .quad = > .LDST128_ABS_LO12_NC ,
310
+ .scalable , .predicate = > unreachable ,
311
+ },
312
+ };
313
+ try atom .addReloc (gpa , .{
314
+ .r_offset = offset ,
315
+ .r_info = @as (u64 , sym_index ) << 32 | @intFromEnum (r_type ),
316
+ .r_addend = @bitCast (addend ),
317
+ }, zo );
318
+ } else if (lf .cast (.macho )) | mf | {
319
+ const zo = mf .getZigObject ().? ;
320
+ const atom = zo .symbols .items [try zo .getOrCreateMetadataForNav (mf , owner_nav )].getAtom (mf ).? ;
321
+ try atom .addReloc (mf , .{
322
+ .tag = .@"extern" ,
323
+ .offset = offset ,
324
+ .target = sym_index ,
325
+ .addend = @bitCast (addend ),
326
+ .type = .pageoff ,
327
+ .meta = .{
328
+ .pcrel = false ,
329
+ .has_subtractor = false ,
330
+ .length = 2 ,
331
+ .symbolnum = @intCast (sym_index ),
332
+ },
333
+ });
334
+ },
262
335
}
263
336
}
264
337
0 commit comments