Skip to content

makefile: update ldflags and add strip for static builds#3054

Closed
kailun-qin wants to merge 1 commit intoopencontainers:masterfrom
kailun-qin:configurable-debug-static
Closed

makefile: update ldflags and add strip for static builds#3054
kailun-qin wants to merge 1 commit intoopencontainers:masterfrom
kailun-qin:configurable-debug-static

Conversation

@kailun-qin
Copy link
Copy Markdown
Contributor

@kailun-qin kailun-qin commented Jun 30, 2021

This patch

  • drops the default -w flag for make static, which helps with
    debugging the static runc binary;
  • adds EXTRA_LDFLAGS="-w -s" to script/release.sh to disable DWARF
    generation and symbol table for the release runc binary;
  • adds strip in script/release.sh for a further size-optimized release
    runc binary.

Signed-off-by: Kailun Qin kailun.qin@intel.com

@kolyshkin
Copy link
Copy Markdown
Contributor

The flag was added by commit ecd6463, not sure why though. The -w flag is to disable DWARF generation (which, as far as I can see, can be stripped using strip).

I think it's better to drop the flag entirely, and instead add EXTRA_LDFLAGS="-w -s" to script/release.sh instead. Here -s is
to disable symbol table (which further reduces the binary size).

@kailun-qin can you do that? While at it, please don't use markdown in commit subject.

Interestingly, even the binary build with -w -s is strippable further; here is a comparison:

[kir@kir-rhat runc]$ ls -l *runc-static*
-rwxrwxr-x. 1 kir kir 15527744 Jun 30 10:52 runc-static
-rwxrwxr-x. 1 kir kir 11599152 Jun 30 11:01 runc-static-s
-rwxrwxr-x. 1 kir kir 13137864 Jun 30 10:52 runc-static-w
-rwxrwxr-x. 1 kir kir 11599152 Jun 30 10:51 runc-static-w-s
-rwxrwxr-x. 1 kir kir 10169624 Jun 30 10:53 stripped-runc-static
-rwxrwxr-x. 1 kir kir 10169624 Jun 30 11:01 stripped-runc-static-s
-rwxrwxr-x. 1 kir kir 10169624 Jun 30 10:53 stripped-runc-static-w
-rwxrwxr-x. 1 kir kir 10169624 Jun 30 10:53 stripped-runc-static-w-s

(in the listing above the first four items are builds with and without -w and -w, and the last for are stripped versions of those).

@cyphar
Copy link
Copy Markdown
Member

cyphar commented Jul 1, 2021

@kolyshkin GNU strip used to break stack backtraces in Go programs, while -w would reduce the size of the binary without that problem (in fact there are some go-nuts threads from a long time ago where folks were told to not use GNU strip on Go binaries). Now, it seems that this is no longer the case so maybe we can just use strip directly?

@kailun-qin kailun-qin force-pushed the configurable-debug-static branch from 2f5d6d4 to a91092c Compare July 1, 2021 07:02
@kailun-qin kailun-qin changed the title makefile: make -w ldflags configurable for static build makefile: update ldflags for static builds Jul 1, 2021
@kailun-qin
Copy link
Copy Markdown
Contributor Author

@kolyshkin @cyphar Thanks for the comments, updated! I intended to propose to add -s (for release) by default as well.
See if you would like to use strip v.s. -w -s. The -w -s approach would not introduce any dependency, for example strip may not be available on Windows systems.

Comment on lines +36 to +38
local ldflags
prefix="$(mktemp -d)"
ldflags="-w -s"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
local ldflags
prefix="$(mktemp -d)"
ldflags="-w -s"
local ldflags="-w -s"
local prefix
prefix=$(mktemp -d)"

The prefix assignment is separated from declaration as otherwise it would hide an error from mktemp. There can be no error in assigning a string, so no need to do the same for ldflags assignment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kailun-qin please address this one ^^^^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, you already did that, but in a separate commit.

@kolyshkin
Copy link
Copy Markdown
Contributor

Now, it seems that this is no longer the case so maybe we can just use strip directly?

To me it makes sense to use both -w -s and strip. The linker flags are merely to save some CPU and disk I/O.

As a fun experiment, I just tried to upx the resulting binary, slashing its size from 9.7M down to 3.5M. Apparently everything still works, the only downsides are:

  • the startup is 0.1s slower;
  • go version -m <binary> no longer works.

@kailun-qin
Copy link
Copy Markdown
Contributor Author

kailun-qin commented Jul 2, 2021

As a fun experiment, I just tried to upx the resulting binary, slashing its size from 9.7M down to 3.5M. Apparently everything still works, the only downsides are:

  • the startup is 0.1s slower;
  • go version -m <binary> no longer works.

Exactly, upx is usually considered as one of the ultimate approaches (available) for binary size optimization, though may incur some performance trade-offs.

@kailun-qin
Copy link
Copy Markdown
Contributor Author

@kolyshkin @cyphar Would you please kindly take another look when you get a chance? Thanks!

@kolyshkin
Copy link
Copy Markdown
Contributor

@kailun-qin please squash your commits, it's easier to review and in general makes more sense in this case.

The only other nit I see is the word "proposes" in the commit description. Once merged, it's no longer a proposal but a factual change, so please change the description accordingly ("This patch does this and that because ...", or, yet better, "Do this and that
because...").

@kailun-qin kailun-qin force-pushed the configurable-debug-static branch from 19034a4 to ffefa6e Compare July 8, 2021 00:26
@kailun-qin kailun-qin changed the title makefile: update ldflags for static builds makefile: update ldflags and add strip for static builds Jul 8, 2021
@kailun-qin kailun-qin force-pushed the configurable-debug-static branch from ffefa6e to 7824f28 Compare July 8, 2021 00:29
@kailun-qin
Copy link
Copy Markdown
Contributor Author

@kailun-qin please squash your commits, it's easier to review and in general makes more sense in this case.

The only other nit I see is the word "proposes" in the commit description. Once merged, it's no longer a proposal but a factual change, so please change the description accordingly ("This patch does this and that because ...", or, yet better, "Do this and that
because...").

Squashed and updated the commit message. PTAL, thanks!

This patch
* drops the default `-w` flag for `make static`, which helps with
  debugging the static runc binary;
* adds `EXTRA_LDFLAGS="-w -s"` to `script/release.sh` to disable DWARF
  generation and symbol table for the release runc binary;
* adds strip in `script/release.sh` for a further size-optimized release
  runc binary.

Signed-off-by: Kailun Qin <kailun.qin@intel.com>
@kailun-qin kailun-qin requested a review from kolyshkin July 13, 2021 01:20
Copy link
Copy Markdown
Contributor

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kolyshkin
Copy link
Copy Markdown
Contributor

Needs a rebase. I am carrying this in #3099 and have rebased it in there, so I guess we can close this one.

@kolyshkin kolyshkin closed this Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants