bionic: Add crt_pad_segment

crt_pad_segment provides a note of type NT_ANDROID_TYPE_PAD_SEGMENT.
It's intended when present is for the loader to pad segment gaps to
reduce kernel slab memory usage (due to additional vm_area_struct's
for gaps). crt_pad_segment.o retains control to the static linker so
that app developers can opt out (build with different flags) it there
are undesireable side effects.

Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .strtab           STRTAB          0000000000000000 0000f8 000066 00      0   0  1
  [ 2] .text             PROGBITS        0000000000000000 000040 000000 00  AX  0   0  4
  [ 3] .note.GNU-stack   PROGBITS        0000000000000000 000040 000000 00      0   0  1
  [ 4] .note.android.pad_segment NOTE            0000000000000000 000040 00001c 00   A  0   0  4
  [ 5] .rela.note.android.pad_segment RELA            0000000000000000 0000e0 000018 18   I  7   4  8
  [ 6] .debug_line       PROGBITS        0000000000000000 00005c 000052 00      0   0  1
  [ 7] .symtab           SYMTAB          0000000000000000 0000b0 000030 18      1   1  8
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  D (mbind), l (large), p (processor specific)

Bug: 316403210
Test: m -j50 ndk
Test: find out/soong/ndk -name 'crt_pad_segment.o'
Test: readelf -SW crt_pag_segment.o
Change-Id: I94af5e85fd602e7ba5fd56788ae39277368229d8
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
diff --git a/libc/Android.bp b/libc/Android.bp
index fdf34eb..139fd0e 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -2319,6 +2319,17 @@
     defaults: ["crt_defaults"],
 }
 
+cc_object {
+    name: "crt_pad_segment",
+    local_include_dirs: [
+        "include",
+        "private",  // crt_pad_segment.S depends on private/bionic_asm_note.h
+    ],
+    srcs: ["arch-common/bionic/crt_pad_segment.S"],
+
+    defaults: ["crt_defaults"],
+}
+
 cc_library_static {
     name: "note_memtag_heap_async",
     arch: {
diff --git a/libc/arch-common/bionic/crt_pad_segment.S b/libc/arch-common/bionic/crt_pad_segment.S
new file mode 100644
index 0000000..86c730d
--- /dev/null
+++ b/libc/arch-common/bionic/crt_pad_segment.S
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <private/bionic_asm_note.h>
+
+  .section ".note.android.pad_segment", "a", %note
+  .balign 4
+  .long 1f - 0f                       // int32_t namesz
+  .long 3f - 2f                       // int32_t descsz
+  .long NT_ANDROID_TYPE_PAD_SEGMENT   // int32_t type
+0:
+  .asciz "Android"                    // char name[]
+1:
+  .balign 2
+2:
+  .long 1    // 1 indicates to pad p_filesz/p_memsz,
+             // such that the current segment ends
+             // where the next segment begins.
+             // If/when padding becomes the default,
+             // 0 can be used to disable this behavior.
+3:
+  .balign 2
diff --git a/libc/private/bionic_asm_note.h b/libc/private/bionic_asm_note.h
index d8559ff..9a533c0 100644
--- a/libc/private/bionic_asm_note.h
+++ b/libc/private/bionic_asm_note.h
@@ -31,3 +31,4 @@
 #define NT_ANDROID_TYPE_IDENT 1
 #define NT_ANDROID_TYPE_KUSER 3
 #define NT_ANDROID_TYPE_MEMTAG 4
+#define NT_ANDROID_TYPE_PAD_SEGMENT 5