Skip to content

cmd/link: support full relro #45681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cmd/link/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ Flags:
instead of $GOROOT/pkg/$GOOS_$GOARCH.
-k symbol
Set field tracking symbol. Use this flag when GOEXPERIMENT=fieldtrack is set.
-bindnow
When linking externally and marking variables read-only after relocation, also require
immediate function binding (default true)
-libgcc file
Set name of compiler support library.
This is only used in internal link mode.
Expand Down
19 changes: 15 additions & 4 deletions src/cmd/link/internal/ld/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,17 @@ func (ctxt *Link) hostlink() {
return argv
}

// Enables Full/Partial RELRO.
addRELROargs := func(argv []string) []string {
relro := "-Wl,-z,relro"
// Enable Full RELRO
if *FlagBindNow {
relro += ",-z,now"
}
argv = append(argv, relro)
return argv
}

switch ctxt.BuildMode {
case BuildModeExe:
if ctxt.HeadType == objabi.Hdarwin {
Expand All @@ -1420,7 +1431,7 @@ func (ctxt *Link) hostlink() {
default:
// ELF.
if ctxt.UseRelro() {
argv = append(argv, "-Wl,-z,relro")
argv = addRELROargs(argv)
}
argv = append(argv, "-pie")
}
Expand All @@ -1429,7 +1440,7 @@ func (ctxt *Link) hostlink() {
argv = append(argv, "-dynamiclib")
} else {
if ctxt.UseRelro() {
argv = append(argv, "-Wl,-z,relro")
argv = addRELROargs(argv)
}
argv = append(argv, "-shared")
if ctxt.HeadType == objabi.Hwindows {
Expand All @@ -1444,15 +1455,15 @@ func (ctxt *Link) hostlink() {
}
case BuildModeShared:
if ctxt.UseRelro() {
argv = append(argv, "-Wl,-z,relro")
argv = addRELROargs(argv)
}
argv = append(argv, "-shared")
case BuildModePlugin:
if ctxt.HeadType == objabi.Hdarwin {
argv = append(argv, "-dynamiclib")
} else {
if ctxt.UseRelro() {
argv = append(argv, "-Wl,-z,relro")
argv = addRELROargs(argv)
}
argv = append(argv, "-shared")
}
Expand Down
1 change: 1 addition & 0 deletions src/cmd/link/internal/ld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var (
FlagDebugTextSize = flag.Int("debugtextsize", 0, "debug text section max size")
flagDebugNosplit = flag.Bool("debugnosplit", false, "dump nosplit call graph")
FlagStrictDups = flag.Int("strictdups", 0, "sanity check duplicate symbol contents during object file reading (1=warn 2=err).")
FlagBindNow = flag.Bool("bindnow", true, "bind function calls when linking externally")
FlagRound = flag.Int("R", -1, "set address rounding `quantum`")
FlagTextAddr = flag.Int64("T", -1, "set text segment `address`")
flagEntrySymbol = flag.String("E", "", "set `entry` symbol name")
Expand Down