blob: 4af52ab67b50b5edfbceb9d9199fda786c38c024 [file] [log] [blame]
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001/*
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 Hughes0266ae52014-02-10 17:46:57 -080021#include <link.h>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080022#include <stdint.h>
23#include <sys/auxv.h>
24
Elliott Hughes0d787c12013-04-04 13:46:46 -070025struct abort_msg_t;
26
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080027// When the kernel starts the dynamic linker, it passes a pointer to a block
28// of memory containing argc, the argv array, the environment variable array,
29// and the array of ELF aux vectors. This class breaks that block up into its
30// constituents for easy access.
31class KernelArgumentBlock {
32 public:
33 KernelArgumentBlock(void* raw_args) {
Elliott Hughesc6200592013-09-30 18:43:46 -070034 uintptr_t* args = reinterpret_cast<uintptr_t*>(raw_args);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080035 argc = static_cast<int>(*args);
36 argv = reinterpret_cast<char**>(args + 1);
37 envp = argv + argc + 1;
38
39 // Skip over all environment variable definitions to find aux vector.
40 // The end of the environment block is marked by two NULL pointers.
41 char** p = envp;
42 while (*p != NULL) {
43 ++p;
44 }
45 ++p; // Skip second NULL;
46
Elliott Hughes0266ae52014-02-10 17:46:57 -080047 auxv = reinterpret_cast<ElfW(auxv_t)*>(p);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080048 }
49
50 // Similar to ::getauxval but doesn't require the libc global variables to be set up,
51 // so it's safe to call this really early on. This function also lets you distinguish
52 // between the inability to find the given type and its value just happening to be 0.
53 unsigned long getauxval(unsigned long type, bool* found_match = NULL) {
Elliott Hughes0266ae52014-02-10 17:46:57 -080054 for (ElfW(auxv_t)* v = auxv; v->a_type != AT_NULL; ++v) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080055 if (v->a_type == type) {
56 if (found_match != NULL) {
57 *found_match = true;
58 }
59 return v->a_un.a_val;
60 }
61 }
62 if (found_match != NULL) {
63 *found_match = false;
64 }
65 return 0;
66 }
67
68 int argc;
69 char** argv;
70 char** envp;
Elliott Hughes0266ae52014-02-10 17:46:57 -080071 ElfW(auxv_t)* auxv;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080072
Elliott Hughes0d787c12013-04-04 13:46:46 -070073 abort_msg_t** abort_message_ptr;
74
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080075 private:
76 // Disallow copy and assignment.
77 KernelArgumentBlock(const KernelArgumentBlock&);
78 void operator=(const KernelArgumentBlock&);
79};
80
81#endif // KERNEL_ARGUMENT_BLOCK_H