diff options
author | 2021-07-27 16:11:51 -0700 | |
---|---|---|
committer | 2021-07-27 22:47:22 -0700 | |
commit | 5c1804831d3c4591cd99299ee12b289b36b23107 (patch) | |
tree | 8bfae1cb7d36beac086d9855040223f8a784d889 | |
parent | 4102c0cc47f58c5278852641ab1636920a59ab48 (diff) |
Add symbols for start and end of embedded linker
Add __dlwrap_linker and __dlwrap_linker_end symbols for use by the
embedded linker trampoline to determine which load segments are
part of the embedded linker and should not be exposed to the
executable.
Use flags to name the sections to avoid colliding with __dlwrap_linker
when no section name can be determined.
Bug: 190084016
Test: build with musl
Change-Id: I51ccbf9c15a65c6194fa263b7504a598dbf4ad18
-rw-r--r-- | cmd/extract_linker/main.go | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/cmd/extract_linker/main.go b/cmd/extract_linker/main.go index f1f7bc74b..1280553a5 100644 --- a/cmd/extract_linker/main.go +++ b/cmd/extract_linker/main.go @@ -26,7 +26,7 @@ import ( "io/ioutil" "log" "os" - "strings" + "strconv" ) func main() { @@ -59,20 +59,16 @@ func main() { fmt.Fprintln(script, "ENTRY(__dlwrap__start)") fmt.Fprintln(script, "SECTIONS {") + progsWithFlagsCount := make(map[string]int) + for _, prog := range ef.Progs { if prog.Type != elf.PT_LOAD { continue } - var progName string - progSection := progToFirstSection(prog, ef.Sections) - if progSection != nil { - progName = progSection.Name - } else { - progName = fmt.Sprintf(".sect%d", load) - } - sectionName := ".linker" + progName - symName := "__dlwrap_linker" + strings.ReplaceAll(progName, ".", "_") + progName := progNameFromFlags(prog.Flags, progsWithFlagsCount) + sectionName := ".linker_" + progName + symName := "__dlwrap_linker_" + progName flags := "" if prog.Flags&elf.PF_W != 0 { @@ -83,6 +79,12 @@ func main() { } fmt.Fprintf(asm, ".section %s, \"a%s\"\n", sectionName, flags) + if load == 0 { + fmt.Fprintln(asm, ".globl __dlwrap_linker") + fmt.Fprintln(asm, "__dlwrap_linker:") + fmt.Fprintln(asm) + } + fmt.Fprintf(asm, ".globl %s\n%s:\n\n", symName, symName) fmt.Fprintf(script, " %s 0x%x : {\n", sectionName, baseLoadAddr+prog.Vaddr) @@ -106,6 +108,10 @@ func main() { load += 1 } + fmt.Fprintln(asm, ".globl __dlwrap_linker_end") + fmt.Fprintln(asm, "__dlwrap_linker_end:") + fmt.Fprintln(asm) + fmt.Fprintln(asm, `.section .note.android.embedded_linker,"a",%note`) fmt.Fprintln(script, "}") @@ -139,11 +145,25 @@ func bytesToAsm(asm io.Writer, buf []byte) { fmt.Fprintln(asm) } -func progToFirstSection(prog *elf.Prog, sections []*elf.Section) *elf.Section { - for _, section := range sections { - if section.Addr == prog.Vaddr { - return section - } +func progNameFromFlags(flags elf.ProgFlag, progsWithFlagsCount map[string]int) string { + s := "" + if flags&elf.PF_R != 0 { + s += "r" + } + if flags&elf.PF_W != 0 { + s += "w" } - return nil + if flags&elf.PF_X != 0 { + s += "x" + } + + count := progsWithFlagsCount[s] + count++ + progsWithFlagsCount[s] = count + + if count > 1 { + s += strconv.Itoa(count) + } + + return s } |