Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5c180483 authored by Colin Cross's avatar Colin Cross
Browse files

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
parent 4102c0cc
Loading
Loading
Loading
Loading
+36 −16
Original line number Diff line number Diff line
@@ -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"
	}
	if flags&elf.PF_X != 0 {
		s += "x"
	}

	count := progsWithFlagsCount[s]
	count++
	progsWithFlagsCount[s] = count

	if count > 1 {
		s += strconv.Itoa(count)
	}
	return nil

	return s
}