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

View File

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