| ... | ... | @@ -62,6 +62,8 @@ debug_rnglists_index: ?Symbol.Index = null, |
| 62 | 62 | eh_frame_index: ?Symbol.Index = null, |
| 63 | 63 | bss_index: ?Symbol.Index = null, |
| 64 | 64 | data_index: ?Symbol.Index = null, |
| 65 | data_relro_index: ?Symbol.Index = null, |
| 66 | rodata_index: ?Symbol.Index = null, |
| 65 | 67 | |
| 66 | 68 | pub const global_symbol_bit: u32 = 0x80000000; |
| 67 | 69 | pub const symbol_mask: u32 = 0x7fffffff; |
| ... | ... | @@ -121,21 +123,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 121 | 123 | .flags = elf.PF_X | elf.PF_R | elf.PF_W, |
| 122 | 124 | }); |
| 123 | 125 | } |
| 124 | | |
| 125 | | if (elf_file.phdr_zig_load_ro_index == null) { |
| 126 | | const alignment = elf_file.page_size; |
| 127 | | const filesz: u64 = 1024; |
| 128 | | const off = try elf_file.findFreeSpace(filesz, alignment); |
| 129 | | elf_file.phdr_zig_load_ro_index = try elf_file.addPhdr(.{ |
| 130 | | .type = elf.PT_LOAD, |
| 131 | | .offset = off, |
| 132 | | .filesz = filesz, |
| 133 | | .addr = if (ptr_size >= 4) 0xc000000 else 0xa000, |
| 134 | | .memsz = filesz, |
| 135 | | .@"align" = alignment, |
| 136 | | .flags = elf.PF_R | elf.PF_W, |
| 137 | | }); |
| 138 | | } |
| 139 | 126 | } |
| 140 | 127 | |
| 141 | 128 | if (elf_file.zig_text_section_index == null) { |
| ... | ... | @@ -159,27 +146,6 @@ pub fn init(self: *ZigObject, elf_file: *Elf, options: InitOptions) !void { |
| 159 | 146 | } |
| 160 | 147 | } |
| 161 | 148 | |
| 162 | | if (elf_file.zig_data_rel_ro_section_index == null) { |
| 163 | | elf_file.zig_data_rel_ro_section_index = try elf_file.addSection(.{ |
| 164 | | .name = try elf_file.insertShString(".data.rel.ro.zig"), |
| 165 | | .type = elf.SHT_PROGBITS, |
| 166 | | .addralign = 1, |
| 167 | | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 168 | | .offset = std.math.maxInt(u64), |
| 169 | | }); |
| 170 | | const shdr = &elf_file.sections.items(.shdr)[elf_file.zig_data_rel_ro_section_index.?]; |
| 171 | | const phndx = &elf_file.sections.items(.phndx)[elf_file.zig_data_rel_ro_section_index.?]; |
| 172 | | try fillSection(elf_file, shdr, 1024, elf_file.phdr_zig_load_ro_index); |
| 173 | | if (elf_file.base.isRelocatable()) { |
| 174 | | _ = try elf_file.addRelaShdr( |
| 175 | | try elf_file.insertShString(".rela.data.rel.ro.zig"), |
| 176 | | elf_file.zig_data_rel_ro_section_index.?, |
| 177 | | ); |
| 178 | | } else { |
| 179 | | phndx.* = elf_file.phdr_zig_load_ro_index.?; |
| 180 | | } |
| 181 | | } |
| 182 | | |
| 183 | 149 | switch (comp.config.debug_format) { |
| 184 | 150 | .strip => {}, |
| 185 | 151 | .dwarf => |v| { |
| ... | ... | @@ -1083,6 +1049,20 @@ pub fn lowerUav( |
| 1083 | 1049 | return .{ .mcv = .{ .load_symbol = metadata.symbol_index } }; |
| 1084 | 1050 | } |
| 1085 | 1051 | |
| 1052 | const osec = if (self.data_relro_index) |sym_index| |
| 1053 | self.symbol(sym_index).atom(elf_file).?.output_section_index |
| 1054 | else osec: { |
| 1055 | const osec = try elf_file.addSection(.{ |
| 1056 | .name = try elf_file.insertShString(".data.rel.ro"), |
| 1057 | .type = elf.SHT_PROGBITS, |
| 1058 | .addralign = 1, |
| 1059 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1060 | .offset = std.math.maxInt(u64), |
| 1061 | }); |
| 1062 | self.data_relro_index = osec; |
| 1063 | break :osec osec; |
| 1064 | }; |
| 1065 | |
| 1086 | 1066 | var name_buf: [32]u8 = undefined; |
| 1087 | 1067 | const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{ |
| 1088 | 1068 | @intFromEnum(uav), |
| ... | ... | @@ -1093,7 +1073,7 @@ pub fn lowerUav( |
| 1093 | 1073 | name, |
| 1094 | 1074 | val, |
| 1095 | 1075 | uav_alignment, |
| 1096 | | elf_file.zig_data_rel_ro_section_index.?, |
| 1076 | osec, |
| 1097 | 1077 | src_loc, |
| 1098 | 1078 | ) catch |err| switch (err) { |
| 1099 | 1079 | error.OutOfMemory => return error.OutOfMemory, |
| ... | ... | @@ -1240,7 +1220,19 @@ fn getNavShdrIndex( |
| 1240 | 1220 | .offset = std.math.maxInt(u64), |
| 1241 | 1221 | }); |
| 1242 | 1222 | } |
| 1243 | | if (is_const) return elf_file.zig_data_rel_ro_section_index.?; |
| 1223 | if (is_const) { |
| 1224 | if (self.data_relro_index) |symbol_index| |
| 1225 | return self.symbol(symbol_index).atom(elf_file).?.output_section_index; |
| 1226 | const osec = try elf_file.addSection(.{ |
| 1227 | .name = try elf_file.insertShString(".data.rel.ro"), |
| 1228 | .type = elf.SHT_PROGBITS, |
| 1229 | .addralign = 1, |
| 1230 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1231 | .offset = std.math.maxInt(u64), |
| 1232 | }); |
| 1233 | self.data_relro_index = try self.addSectionSymbol(gpa, ".data.rel.ro", .@"1", osec); |
| 1234 | return osec; |
| 1235 | } |
| 1244 | 1236 | if (nav_init != .none and Value.fromInterned(nav_init).isUndefDeep(zcu)) |
| 1245 | 1237 | return switch (zcu.navFileScope(nav_index).mod.optimize_mode) { |
| 1246 | 1238 | .Debug, .ReleaseSafe => { |
| ... | ... | @@ -1253,7 +1245,12 @@ fn getNavShdrIndex( |
| 1253 | 1245 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1254 | 1246 | .offset = std.math.maxInt(u64), |
| 1255 | 1247 | }); |
| 1256 | | self.data_index = try self.addSectionSymbol(gpa, ".data", .@"1", osec); |
| 1248 | self.data_index = try self.addSectionSymbol( |
| 1249 | gpa, |
| 1250 | ".data", |
| 1251 | Atom.Alignment.fromNonzeroByteUnits(ptr_size), |
| 1252 | osec, |
| 1253 | ); |
| 1257 | 1254 | return osec; |
| 1258 | 1255 | }, |
| 1259 | 1256 | .ReleaseFast, .ReleaseSmall => { |
| ... | ... | @@ -1293,7 +1290,12 @@ fn getNavShdrIndex( |
| 1293 | 1290 | .flags = elf.SHF_ALLOC | elf.SHF_WRITE, |
| 1294 | 1291 | .offset = std.math.maxInt(u64), |
| 1295 | 1292 | }); |
| 1296 | | self.data_index = try self.addSectionSymbol(gpa, ".data", .@"1", osec); |
| 1293 | self.data_index = try self.addSectionSymbol( |
| 1294 | gpa, |
| 1295 | ".data", |
| 1296 | Atom.Alignment.fromNonzeroByteUnits(ptr_size), |
| 1297 | osec, |
| 1298 | ); |
| 1297 | 1299 | return osec; |
| 1298 | 1300 | } |
| 1299 | 1301 | |
| ... | ... | @@ -1702,7 +1704,19 @@ fn updateLazySymbol( |
| 1702 | 1704 | |
| 1703 | 1705 | const output_section_index = switch (sym.kind) { |
| 1704 | 1706 | .code => elf_file.zig_text_section_index.?, |
| 1705 | | .const_data => elf_file.zig_data_rel_ro_section_index.?, |
| 1707 | .const_data => if (self.rodata_index) |sym_index| |
| 1708 | self.symbol(sym_index).atom(elf_file).?.output_section_index |
| 1709 | else osec: { |
| 1710 | const osec = try elf_file.addSection(.{ |
| 1711 | .name = try elf_file.insertShString(".rodata"), |
| 1712 | .type = elf.SHT_PROGBITS, |
| 1713 | .addralign = 1, |
| 1714 | .flags = elf.SHF_ALLOC, |
| 1715 | .offset = std.math.maxInt(u64), |
| 1716 | }); |
| 1717 | self.rodata_index = try self.addSectionSymbol(gpa, ".rodata", .@"1", osec); |
| 1718 | break :osec osec; |
| 1719 | }, |
| 1706 | 1720 | }; |
| 1707 | 1721 | const local_sym = self.symbol(symbol_index); |
| 1708 | 1722 | local_sym.name_offset = name_str_index; |
| ... | ... | @@ -2003,7 +2017,10 @@ fn allocateAtom(self: *ZigObject, atom_ptr: *Atom, elf_file: *Elf) !void { |
| 2003 | 2017 | } |
| 2004 | 2018 | shdr.sh_addralign = @max(shdr.sh_addralign, atom_ptr.alignment.toByteUnits().?); |
| 2005 | 2019 | |
| 2006 | | const sect_atom_ptr = for ([_]?Symbol.Index{self.data_index}) |maybe_sym_index| { |
| 2020 | const sect_atom_ptr = for ([_]?Symbol.Index{ |
| 2021 | self.data_index, |
| 2022 | self.data_relro_index, |
| 2023 | }) |maybe_sym_index| { |
| 2007 | 2024 | const sect_sym_index = maybe_sym_index orelse continue; |
| 2008 | 2025 | const sect_atom_ptr = self.symbol(sect_sym_index).atom(elf_file).?; |
| 2009 | 2026 | if (sect_atom_ptr.output_section_index == atom_ptr.output_section_index) break sect_atom_ptr; |