Merge "Remove unused includes and `extern`s." into main
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 7c09e88..31651cd 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -1,32 +1,37 @@
 # This file is used to automatically generate bionic's system call stubs.
 #
+# It is processed by a python script named gensyscalls.py,
+# normally run via the genrules in libc/Android.bp.
+#
 # Each non-blank, non-comment line has the following format:
 #
-# func_name[|alias_list][:syscall_name[:socketcall_id]]([parameter_list]) arch_list
+#     func_name[|alias_list][:syscall_name[:socketcall_id]]([parameter_list]) arch_list
 #
 # where:
-#       arch_list ::= "all" | arches
-#       arches    ::= arch |  arch "," arches
-#       arch      ::= "arm" | "arm64" | "riscv64" | "x86" | "x86_64" | "lp32" | "lp64"
+#     arch_list ::= "all" | arches
+#     arches    ::= arch |  arch "," arches
+#     arch      ::= "arm" | "arm64" | "riscv64" | "x86" | "x86_64" | "lp32" | "lp64"
 #
-# Note:
-#      - syscall_name corresponds to the name of the syscall, which may differ from
-#        the exported function name (example: the exit syscall is implemented by the _exit()
-#        function, which is not the same as the standard C exit() function which calls it)
+# syscall_name corresponds to the name of the syscall, which may differ from
+# the exported function name func_name. For example: the exit_group syscall
+# is exported by libc as the _exit() function, not exit() (which does more
+# work before calling _exit()).
 #
-#      - alias_list is optional comma separated list of function aliases.
+# alias_list is optional comma-separated list of function aliases.
+# For example, the traditional _exit() function has a C99 alias _Exit().
 #
-#      - No return type is specified.
+# No return type is specified, because it's not needed.
 #
-#      - The call_id parameter, given that func_name and syscall_name have
-#        been provided, allows the user to specify dispatch style syscalls.
-#        For example, socket() syscall on i386 actually becomes:
-#          socketcall(__NR_socket, 1, *(rest of args on stack)).
+# The socketcall_id parameter supports x86's
+# https://man7.org/linux/man-pages/man2/socketcall.2.html
+# and can be ignored for all other syscalls and architectures.
 #
-#      - Each parameter type is assumed to be stored in 32 bits.
+# The number of registers required for the arguments is computed by the script,
+# based on the parameter list given here. It handles the need for register
+# pairs for 64-bit arguments on ILP32, and also arm32's requirement for such
+# pairs to start on an even register. This means that it's important to get
+# these types right!
 #
-# This file is processed by a python script named gensyscalls.py, run via
-# genrules in Android.bp.
 
 # Calls that have historical 16-bit variants camping on the best names (CONFIG_UID16).
 getuid:getuid32()   lp32
diff --git a/libc/tools/gensyscalls.py b/libc/tools/gensyscalls.py
index aa4f1cb..d371b72 100755
--- a/libc/tools/gensyscalls.py
+++ b/libc/tools/gensyscalls.py
@@ -27,6 +27,7 @@
 # ARM assembler templates for each syscall stub
 #
 
+# ARM assembler template for a syscall stub needing 4 or fewer registers
 arm_call_default = syscall_stub_header + """\
     mov     ip, r7
     .cfi_register r7, ip
@@ -41,6 +42,7 @@
 END(%(func)s)
 """
 
+# ARM assembler template for a syscall stub needing more than 4 registers
 arm_call_long = syscall_stub_header + """\
     mov     ip, sp
     stmfd   sp!, {r4, r5, r6, r7}
@@ -165,13 +167,13 @@
 
     # Second, check that there is no pointer type here
     if param.find("*") >= 0:
-            return False
+        return False
 
     # Ok
     return True
 
 
-def count_arm_param_registers(params):
+def count_param_registers_arm32(params):
     """This function is used to count the number of register used
        to pass parameters when invoking an ARM system call.
        This is because the ARM EABI mandates that 64-bit quantities
@@ -196,7 +198,7 @@
     return count
 
 
-def count_generic_param_registers(params):
+def count_param_registers_x86(params):
     count = 0
     for param in params:
         if param_uses_64bits(param):
@@ -206,13 +208,6 @@
     return count
 
 
-def count_generic_param_registers64(params):
-    count = 0
-    for param in params:
-        count += 1
-    return count
-
-
 # This lets us support regular system calls like __NR_write and also weird
 # ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
 def make__NR_name(name):
@@ -231,7 +226,7 @@
 
 
 def arm_genstub(syscall):
-    num_regs = count_arm_param_registers(syscall["params"])
+    num_regs = count_param_registers_arm32(syscall["params"])
     if num_regs > 4:
         return arm_call_long % syscall
     return arm_call_default % syscall
@@ -248,7 +243,7 @@
 def x86_genstub(syscall):
     result     = syscall_stub_header % syscall
 
-    numparams = count_generic_param_registers(syscall["params"])
+    numparams = count_param_registers_x86(syscall["params"])
     stack_bias = numparams*4 + 8
     offset = 0
     mov_result = ""
@@ -316,7 +311,7 @@
 
 def x86_64_genstub(syscall):
     result = syscall_stub_header % syscall
-    num_regs = count_generic_param_registers64(syscall["params"])
+    num_regs = len(syscall["params"])
     if (num_regs > 3):
         # rcx is used as 4th argument. Kernel wants it at r10.
         result += "    movq    %rcx, %r10\n"
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 2393f13..2db6e81 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -346,7 +346,7 @@
     }
   }
 
-  if (si->has_min_version(6) && si->get_gap_size()) {
+  if (si->is_lp64_or_has_min_version(6) && si->get_gap_size()) {
     munmap(reinterpret_cast<void*>(si->get_gap_start()), si->get_gap_size());
   }
 
@@ -1907,7 +1907,7 @@
 
     local_unload_list.push_back(si);
 
-    if (si->has_min_version(0)) {
+    if (si->is_lp64_or_has_min_version(0)) {
       soinfo* child = nullptr;
       while ((child = si->get_children().pop_front()) != nullptr) {
         LD_DEBUG(any, "%s@%p needs to unload %s@%p", si->get_realpath(), si,
@@ -2671,7 +2671,7 @@
 
 template <typename F>
 static bool for_each_verdef(const soinfo* si, F functor) {
-  if (!si->has_min_version(2)) {
+  if (!si->is_lp64_or_has_min_version(2)) {
     return true;
   }
 
@@ -2762,7 +2762,7 @@
 }
 
 bool VersionTracker::init(const soinfo* si_from) {
-  if (!si_from->has_min_version(2)) {
+  if (!si_from->is_lp64_or_has_min_version(2)) {
     return true;
   }
 
diff --git a/linker/linker_namespaces.cpp b/linker/linker_namespaces.cpp
index eb9dae9..55168fd 100644
--- a/linker/linker_namespaces.cpp
+++ b/linker/linker_namespaces.cpp
@@ -73,7 +73,7 @@
   auto is_accessible_ftor = [this] (soinfo* si, bool allow_secondary) {
     // This is workaround for apps hacking into soinfo list.
     // and inserting their own entries into it. (http://b/37191433)
-    if (!si->has_min_version(3)) {
+    if (!si->is_lp64_or_has_min_version(3)) {
       DL_WARN("Warning: invalid soinfo version for \"%s\" (assuming inaccessible)",
               si->get_soname());
       return false;
diff --git a/linker/linker_relocate.cpp b/linker/linker_relocate.cpp
index bbf8359..94281cb 100644
--- a/linker/linker_relocate.cpp
+++ b/linker/linker_relocate.cpp
@@ -605,7 +605,7 @@
   Relocator relocator(version_tracker, lookup_list);
   relocator.si = this;
   relocator.si_strtab = strtab_;
-  relocator.si_strtab_size = has_min_version(1) ? strtab_size_ : SIZE_MAX;
+  relocator.si_strtab_size = is_lp64_or_has_min_version(1) ? strtab_size_ : SIZE_MAX;
   relocator.si_symtab = symtab_;
   relocator.tlsdesc_args = &tlsdesc_args_;
   relocator.tls_tp_base = __libc_shared_globals()->static_tls_layout.offset_thread_pointer();
diff --git a/linker/linker_soinfo.cpp b/linker/linker_soinfo.cpp
index 27a5237..b3b9da3 100644
--- a/linker/linker_soinfo.cpp
+++ b/linker/linker_soinfo.cpp
@@ -217,7 +217,7 @@
 }
 
 void soinfo::set_dt_runpath(const char* path) {
-  if (!has_min_version(3)) {
+  if (!is_lp64_or_has_min_version(3)) {
     return;
   }
 
@@ -244,35 +244,19 @@
 }
 
 ElfW(Addr) soinfo::get_verneed_ptr() const {
-  if (has_min_version(2)) {
-    return verneed_ptr_;
-  }
-
-  return 0;
+  return is_lp64_or_has_min_version(2)? verneed_ptr_ : 0;
 }
 
 size_t soinfo::get_verneed_cnt() const {
-  if (has_min_version(2)) {
-    return verneed_cnt_;
-  }
-
-  return 0;
+  return is_lp64_or_has_min_version(2) ? verneed_cnt_ : 0;
 }
 
 ElfW(Addr) soinfo::get_verdef_ptr() const {
-  if (has_min_version(2)) {
-    return verdef_ptr_;
-  }
-
-  return 0;
+  return is_lp64_or_has_min_version(2) ? verdef_ptr_ : 0;
 }
 
 size_t soinfo::get_verdef_cnt() const {
-  if (has_min_version(2)) {
-    return verdef_cnt_;
-  }
-
-  return 0;
+  return is_lp64_or_has_min_version(2) ? verdef_cnt_ : 0;
 }
 
 SymbolLookupLib soinfo::get_lookup_lib() {
@@ -530,14 +514,14 @@
 }
 
 void soinfo::add_child(soinfo* child) {
-  if (has_min_version(0)) {
+  if (is_lp64_or_has_min_version(0)) {
     child->parents_.push_back(this);
     this->children_.push_back(child);
   }
 }
 
 void soinfo::remove_all_links() {
-  if (!has_min_version(0)) {
+  if (!is_lp64_or_has_min_version(0)) {
     return;
   }
 
@@ -571,47 +555,27 @@
 }
 
 dev_t soinfo::get_st_dev() const {
-  if (has_min_version(0)) {
-    return st_dev_;
-  }
-
-  return 0;
+  return is_lp64_or_has_min_version(0) ? st_dev_ : 0;
 };
 
 ino_t soinfo::get_st_ino() const {
-  if (has_min_version(0)) {
-    return st_ino_;
-  }
-
-  return 0;
+  return is_lp64_or_has_min_version(0) ? st_ino_ : 0;
 }
 
 off64_t soinfo::get_file_offset() const {
-  if (has_min_version(1)) {
-    return file_offset_;
-  }
-
-  return 0;
+  return is_lp64_or_has_min_version(1) ? file_offset_ : 0;
 }
 
 uint32_t soinfo::get_rtld_flags() const {
-  if (has_min_version(1)) {
-    return rtld_flags_;
-  }
-
-  return 0;
+  return is_lp64_or_has_min_version(1) ? rtld_flags_ : 0;
 }
 
 uint32_t soinfo::get_dt_flags_1() const {
-  if (has_min_version(1)) {
-    return dt_flags_1_;
-  }
-
-  return 0;
+  return is_lp64_or_has_min_version(1) ? dt_flags_1_ : 0;
 }
 
 void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
-  if (has_min_version(1)) {
+  if (is_lp64_or_has_min_version(1)) {
     if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
       rtld_flags_ |= RTLD_GLOBAL;
     }
@@ -629,34 +593,24 @@
 }
 
 void soinfo::set_realpath(const char* path) {
-#if defined(__LP64__)
-  realpath_ = path;
-#else
-  if (has_min_version(2)) {
+  if (is_lp64_or_has_min_version(2)) {
     realpath_ = path;
   }
-#endif
 }
 
 const char* soinfo::get_realpath() const {
 #if defined(__LP64__)
   return realpath_.c_str();
 #else
-  if (has_min_version(2)) {
-    return realpath_.c_str();
-  } else {
-    return old_name_;
-  }
+  return is_lp64_or_has_min_version(2) ? realpath_.c_str() : old_name_;
 #endif
 }
 
 void soinfo::set_soname(const char* soname) {
-#if defined(__LP64__)
-  soname_ = soname;
-#else
-  if (has_min_version(2)) {
+  if (is_lp64_or_has_min_version(2)) {
     soname_ = soname;
   }
+#if !defined(__LP64__)
   strlcpy(old_name_, soname_.c_str(), sizeof(old_name_));
 #endif
 }
@@ -665,11 +619,7 @@
 #if defined(__LP64__)
   return soname_.c_str();
 #else
-  if (has_min_version(2)) {
-    return soname_.c_str();
-  } else {
-    return old_name_;
-  }
+  return is_lp64_or_has_min_version(2) ? soname_.c_str() : old_name_;
 #endif
 }
 
@@ -678,59 +628,39 @@
 static soinfo_list_t g_empty_list;
 
 soinfo_list_t& soinfo::get_children() {
-  if (has_min_version(0)) {
-    return children_;
-  }
-
-  return g_empty_list;
+  return is_lp64_or_has_min_version(0) ? children_ : g_empty_list;
 }
 
 const soinfo_list_t& soinfo::get_children() const {
-  if (has_min_version(0)) {
-    return children_;
-  }
-
-  return g_empty_list;
+  return is_lp64_or_has_min_version(0) ? children_ : g_empty_list;
 }
 
 soinfo_list_t& soinfo::get_parents() {
-  if (has_min_version(0)) {
-    return parents_;
-  }
-
-  return g_empty_list;
+  return is_lp64_or_has_min_version(0) ? parents_ : g_empty_list;
 }
 
 static std::vector<std::string> g_empty_runpath;
 
 const std::vector<std::string>& soinfo::get_dt_runpath() const {
-  if (has_min_version(3)) {
-    return dt_runpath_;
-  }
-
-  return g_empty_runpath;
+  return is_lp64_or_has_min_version(3) ? dt_runpath_ : g_empty_runpath;
 }
 
 android_namespace_t* soinfo::get_primary_namespace() {
-  if (has_min_version(3)) {
-    return primary_namespace_;
-  }
-
-  return &g_default_namespace;
+  return is_lp64_or_has_min_version(3) ? primary_namespace_ : &g_default_namespace;
 }
 
 void soinfo::add_secondary_namespace(android_namespace_t* secondary_ns) {
-  CHECK(has_min_version(3));
+  CHECK(is_lp64_or_has_min_version(3));
   secondary_namespaces_.push_back(secondary_ns);
 }
 
 android_namespace_list_t& soinfo::get_secondary_namespaces() {
-  CHECK(has_min_version(3));
+  CHECK(is_lp64_or_has_min_version(3));
   return secondary_namespaces_;
 }
 
 const char* soinfo::get_string(ElfW(Word) index) const {
-  if (has_min_version(1) && (index >= strtab_size_)) {
+  if (is_lp64_or_has_min_version(1) && (index >= strtab_size_)) {
     async_safe_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
         get_realpath(), strtab_size_, index);
   }
@@ -813,7 +743,7 @@
 // dlopen/load. Note that libraries opened by system
 // will always have 'current' target sdk version.
 int soinfo::get_target_sdk_version() const {
-  if (!has_min_version(2)) {
+  if (!is_lp64_or_has_min_version(2)) {
     return __ANDROID_API__;
   }
 
@@ -821,13 +751,13 @@
 }
 
 uintptr_t soinfo::get_handle() const {
-  CHECK(has_min_version(3));
+  CHECK(is_lp64_or_has_min_version(3));
   CHECK(handle_ != 0);
   return handle_;
 }
 
 void* soinfo::to_handle() {
-  if (get_application_target_sdk_version() < 24 || !has_min_version(3)) {
+  if (get_application_target_sdk_version() < 24 || !is_lp64_or_has_min_version(3)) {
     return this;
   }
 
@@ -835,7 +765,7 @@
 }
 
 void soinfo::generate_handle() {
-  CHECK(has_min_version(3));
+  CHECK(is_lp64_or_has_min_version(3));
   CHECK(handle_ == 0); // Make sure this is the first call
 
   // Make sure the handle is unique and does not collide
@@ -861,20 +791,20 @@
 }
 
 void soinfo::set_gap_start(ElfW(Addr) gap_start) {
-  CHECK(has_min_version(6));
+  CHECK(is_lp64_or_has_min_version(6));
   gap_start_ = gap_start;
 }
 ElfW(Addr) soinfo::get_gap_start() const {
-  CHECK(has_min_version(6));
+  CHECK(is_lp64_or_has_min_version(6));
   return gap_start_;
 }
 
 void soinfo::set_gap_size(size_t gap_size) {
-  CHECK(has_min_version(6));
+  CHECK(is_lp64_or_has_min_version(6));
   gap_size_ = gap_size;
 }
 size_t soinfo::get_gap_size() const {
-  CHECK(has_min_version(6));
+  CHECK(is_lp64_or_has_min_version(6));
   return gap_size_;
 }
 
diff --git a/linker/linker_soinfo.h b/linker/linker_soinfo.h
index 3416221..8f56c9f 100644
--- a/linker/linker_soinfo.h
+++ b/linker/linker_soinfo.h
@@ -154,14 +154,10 @@
   size_t module_id = kTlsUninitializedModuleId;
 };
 
-#if !defined(__LP64__)
-#define SOINFO_NAME_LEN 128
-#endif
-
 struct soinfo {
 #if !defined(__LP64__)
  private:
-  char old_name_[SOINFO_NAME_LEN];
+  char old_name_[128];
 #endif
  public:
   const ElfW(Phdr)* phdr;
@@ -294,8 +290,7 @@
   bool can_unload() const;
   bool is_gnu_hash() const;
 
-  // TODO: rename this to make it clearer it's always true for LP64.
-  bool inline has_min_version(uint32_t min_version __unused) const {
+  inline bool is_lp64_or_has_min_version(uint32_t min_version __unused) const {
 #if defined(__LP64__)
     return true;
 #else
@@ -304,7 +299,7 @@
   }
 
   const ElfW(Versym)* get_versym_table() const {
-    return has_min_version(2) ? versym_ : nullptr;
+    return is_lp64_or_has_min_version(2) ? versym_ : nullptr;
   }
 
   bool is_linked() const;
@@ -341,7 +336,7 @@
   android_namespace_list_t& get_secondary_namespaces();
 
   soinfo_tls* get_tls() const {
-    return has_min_version(5) ? tls_.get() : nullptr;
+    return is_lp64_or_has_min_version(5) ? tls_.get() : nullptr;
   }
 
   void set_mapped_by_caller(bool reserved_map);