1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

native: don't generate duplicate strings (#16281)

This commit is contained in:
Spydr 2022-11-02 14:48:25 +01:00 committed by GitHub
parent 0fa6f60fac
commit 075c025999
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 9 deletions

View File

@ -823,22 +823,29 @@ pub fn (mut g Gen) generate_simple_elf_header() {
}
pub fn (mut g Gen) elf_string_table() {
mut generated := map[string]int{}
for _, s in g.strs {
pos := generated[s.str] or { g.buf.len }
match s.typ {
.abs64 {
// g.write64_at(native.segment_start + g.buf.len, int(g.str_pos[i]))
g.write64_at(s.pos, g.buf.len)
g.write64_at(s.pos, pos)
}
.rel32 {
g.write32_at(s.pos, g.buf.len - s.pos - 4)
g.write32_at(s.pos, pos - s.pos - 4)
}
else {
g.n_error('unsupported string reloc type')
}
}
if s.str !in generated {
generated[s.str] = pos
g.write_string(s.str)
}
}
}
pub fn (mut g Gen) gen_rela_section() {
mut relocations := []RelASection{}

View File

@ -478,22 +478,30 @@ fn (mut g Gen) write_symbol(s Symbol) {
fn (mut g Gen) sym_string_table() int {
begin := g.buf.len
g.zeroes(1)
mut generated := map[string]int{}
for _, s in g.strs {
pos := g.buf.len - s.pos - 4
pos := generated[s.str] or { g.buf.len }
match s.typ {
.rel32 {
g.write32_at(s.pos, pos)
g.write32_at(s.pos, pos - s.pos - 4)
}
else {
if g.pref.os == .windows {
// that should be .rel32, not windows-specific
g.write32_at(s.pos, pos)
g.write32_at(s.pos, pos - s.pos - 4)
} else {
g.write64_at(s.pos, g.buf.len + native.base_addr)
g.write64_at(s.pos, pos + native.base_addr)
}
}
}
if s.str !in generated {
generated[s.str] = pos
g.write_string(s.str)
}
}
return g.buf.len - begin
}