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> |
| 21 | #include <stdint.h> |
| 22 | #include <sys/auxv.h> |
| 23 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 24 | struct abort_msg_t; |
| 25 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 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: |
| 32 | KernelArgumentBlock(void* raw_args) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 33 | uintptr_t* args = reinterpret_cast<uintptr_t*>(raw_args); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 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 aux vector. |
| 39 | // The end of the environment block is marked by two NULL pointers. |
| 40 | char** p = envp; |
| 41 | while (*p != NULL) { |
| 42 | ++p; |
| 43 | } |
| 44 | ++p; // Skip second NULL; |
| 45 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 46 | auxv = reinterpret_cast<Elf_auxv_t*>(p); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 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. This function also lets you distinguish |
| 51 | // between the inability to find the given type and its value just happening to be 0. |
| 52 | unsigned long getauxval(unsigned long type, bool* found_match = NULL) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 53 | for (Elf_auxv_t* v = auxv; v->a_type != AT_NULL; ++v) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 54 | if (v->a_type == type) { |
| 55 | if (found_match != NULL) { |
| 56 | *found_match = true; |
| 57 | } |
| 58 | return v->a_un.a_val; |
| 59 | } |
| 60 | } |
| 61 | if (found_match != NULL) { |
| 62 | *found_match = false; |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int argc; |
| 68 | char** argv; |
| 69 | char** envp; |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 70 | Elf_auxv_t* auxv; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 71 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 72 | abort_msg_t** abort_message_ptr; |
| 73 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 74 | private: |
| 75 | // Disallow copy and assignment. |
| 76 | KernelArgumentBlock(const KernelArgumentBlock&); |
| 77 | void operator=(const KernelArgumentBlock&); |
| 78 | }; |
| 79 | |
| 80 | #endif // KERNEL_ARGUMENT_BLOCK_H |