Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 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 | #ifndef KERNEL_ARGUMENT_BLOCK_H |
| 18 | #define KERNEL_ARGUMENT_BLOCK_H |
| 19 | |
| 20 | #include <elf.h> |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 21 | #include <link.h> |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <sys/auxv.h> |
| 24 | |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 25 | #include "private/bionic_macros.h" |
| 26 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 27 | struct abort_msg_t; |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 28 | struct libc_shared_globals; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 30 | // When the kernel starts the dynamic linker, it passes a pointer to a block |
| 31 | // of memory containing argc, the argv array, the environment variable array, |
| 32 | // and the array of ELF aux vectors. This class breaks that block up into its |
| 33 | // constituents for easy access. |
| 34 | class KernelArgumentBlock { |
| 35 | public: |
Elliott Hughes | 5cec377 | 2018-01-19 15:45:23 -0800 | [diff] [blame] | 36 | explicit KernelArgumentBlock(void* raw_args) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 37 | uintptr_t* args = reinterpret_cast<uintptr_t*>(raw_args); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 38 | argc = static_cast<int>(*args); |
| 39 | argv = reinterpret_cast<char**>(args + 1); |
| 40 | envp = argv + argc + 1; |
| 41 | |
Elliott Hughes | 63fbb23 | 2016-01-05 16:29:33 -0800 | [diff] [blame] | 42 | // Skip over all environment variable definitions to find the aux vector. |
| 43 | // The end of the environment block is marked by a NULL pointer. |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 44 | char** p = envp; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame^] | 45 | while (*p != nullptr) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 46 | ++p; |
| 47 | } |
Elliott Hughes | 63fbb23 | 2016-01-05 16:29:33 -0800 | [diff] [blame] | 48 | ++p; // Skip the NULL itself. |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 50 | auxv = reinterpret_cast<ElfW(auxv_t)*>(p); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Similar to ::getauxval but doesn't require the libc global variables to be set up, |
Elliott Hughes | 63fbb23 | 2016-01-05 16:29:33 -0800 | [diff] [blame] | 54 | // so it's safe to call this really early on. |
| 55 | unsigned long getauxval(unsigned long type) { |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 56 | for (ElfW(auxv_t)* v = auxv; v->a_type != AT_NULL; ++v) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 57 | if (v->a_type == type) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 58 | return v->a_un.a_val; |
| 59 | } |
| 60 | } |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int argc; |
| 65 | char** argv; |
| 66 | char** envp; |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 67 | ElfW(auxv_t)* auxv; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 68 | |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 69 | // Other data that we want to pass from the dynamic linker to libc.so. |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 70 | abort_msg_t** abort_message_ptr; |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 71 | libc_shared_globals* shared_globals; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 72 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 73 | private: |
Elliott Hughes | 8eac9af | 2014-05-09 19:12:08 -0700 | [diff] [blame] | 74 | DISALLOW_COPY_AND_ASSIGN(KernelArgumentBlock); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | #endif // KERNEL_ARGUMENT_BLOCK_H |