1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <elf.h> 20 #include <link.h> 21 #include <stdint.h> 22 #include <sys/auxv.h> 23 24 #include "platform/bionic/macros.h" 25 26 // When the kernel starts the dynamic linker, it passes a pointer to a block 27 // of memory containing argc, the argv array, the environment variable array, 28 // and the array of ELF aux vectors. This class breaks that block up into its 29 // constituents for easy access. 30 class KernelArgumentBlock { 31 public: KernelArgumentBlock(void * raw_args)32 explicit KernelArgumentBlock(void* raw_args) { 33 uintptr_t* args = reinterpret_cast<uintptr_t*>(raw_args); 34 argc = static_cast<int>(*args); 35 argv = reinterpret_cast<char**>(args + 1); 36 envp = argv + argc + 1; 37 38 // Skip over all environment variable definitions to find the aux vector. 39 // The end of the environment block is marked by a NULL pointer. 40 char** p = envp; 41 while (*p != nullptr) { 42 ++p; 43 } 44 ++p; // Skip the NULL itself. 45 46 auxv = reinterpret_cast<ElfW(auxv_t)*>(p); 47 } 48 49 // Similar to ::getauxval but doesn't require the libc global variables to be set up, 50 // so it's safe to call this really early on. getauxval(unsigned long type)51 unsigned long getauxval(unsigned long type) { 52 for (ElfW(auxv_t)* v = auxv; v->a_type != AT_NULL; ++v) { 53 if (v->a_type == type) { 54 return v->a_un.a_val; 55 } 56 } 57 return 0; 58 } 59 60 int argc; 61 char** argv; 62 char** envp; 63 ElfW(auxv_t)* auxv; 64 65 private: 66 BIONIC_DISALLOW_COPY_AND_ASSIGN(KernelArgumentBlock); 67 }; 68