[dev.link] cmd/link: fix end-of-block padding

Make sure we write the entire address range we are asked to write,
with no holes between the blocks or at the end.

Should fix NetBSD build.

Change-Id: I13b1f551377cbc4bcde3650417ac95cba62ff807
Reviewed-on: https://go-review.googlesource.com/c/go/+/226617
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
diff --git a/src/cmd/link/internal/ld/data.go b/src/cmd/link/internal/ld/data.go
index b1c03b9..f7d8d13 100644
--- a/src/cmd/link/internal/ld/data.go
+++ b/src/cmd/link/internal/ld/data.go
@@ -826,7 +826,6 @@
 
 			// We're gonna write this symbol.
 			idx = i
-			length = s.Value + s.Size - addr
 
 			// If we cross over the max size, we've got enough symbols.
 			if s.Value+s.Size > addr+max {
@@ -839,6 +838,13 @@
 			break
 		}
 
+		// Compute the length to write, including padding.
+		if idx+1 < len(syms) {
+			length = syms[idx+1].Value - addr
+		} else {
+			length = lastAddr - addr
+		}
+
 		// Start the block output operator.
 		if o, err := out.View(uint64(out.Offset() + written)); err == nil {
 			sem <- 1