KernelArgumentBlock.h revision 8eac9af24ea7e570e0b297bcd6ac8a46ba3ecc39
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 <link.h>
22#include <stdint.h>
23#include <sys/auxv.h>
24
25#include "private/bionic_macros.h"
26
27struct abort_msg_t;
28
29// When the kernel starts the dynamic linker, it passes a pointer to a block
30// of memory containing argc, the argv array, the environment variable array,
31// and the array of ELF aux vectors. This class breaks that block up into its
32// constituents for easy access.
33class KernelArgumentBlock {
34 public:
35  KernelArgumentBlock(void* raw_args) {
36    uintptr_t* args = reinterpret_cast<uintptr_t*>(raw_args);
37    argc = static_cast<int>(*args);
38    argv = reinterpret_cast<char**>(args + 1);
39    envp = argv + argc + 1;
40
41    // Skip over all environment variable definitions to find aux vector.
42    // The end of the environment block is marked by two NULL pointers.
43    char** p = envp;
44    while (*p != NULL) {
45      ++p;
46    }
47    ++p; // Skip second NULL;
48
49    auxv = reinterpret_cast<ElfW(auxv_t)*>(p);
50  }
51
52  // Similar to ::getauxval but doesn't require the libc global variables to be set up,
53  // so it's safe to call this really early on. This function also lets you distinguish
54  // between the inability to find the given type and its value just happening to be 0.
55  unsigned long getauxval(unsigned long type, bool* found_match = NULL) {
56    for (ElfW(auxv_t)* v = auxv; v->a_type != AT_NULL; ++v) {
57      if (v->a_type == type) {
58        if (found_match != NULL) {
59            *found_match = true;
60        }
61        return v->a_un.a_val;
62      }
63    }
64    if (found_match != NULL) {
65      *found_match = false;
66    }
67    return 0;
68  }
69
70  int argc;
71  char** argv;
72  char** envp;
73  ElfW(auxv_t)* auxv;
74
75  abort_msg_t** abort_message_ptr;
76
77 private:
78  DISALLOW_COPY_AND_ASSIGN(KernelArgumentBlock);
79};
80
81#endif // KERNEL_ARGUMENT_BLOCK_H
82