crt.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2015 Assured Information Security, Inc.
3 // Author: Rian Quinn <quinnr@ainfosec.com>
4 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 #include <gsl/gsl>
21 
22 #include <crt.h>
23 #include <eh_frame_list.h>
24 
25 typedef void (*init_t)();
26 typedef void (*fini_t)();
27 
28 extern "C" int64_t
30 {
31  if (info == nullptr)
32  return CRT_FAILURE;
33 
34  try
35  {
36  if (info->init_addr != nullptr)
37  reinterpret_cast<init_t>(info->init_addr)();
38 
39  if (info->init_array_addr != nullptr)
40  {
41  auto n = static_cast<std::ptrdiff_t>(info->init_array_size >> 3);
42  auto init_array = gsl::make_span(static_cast<init_t *>(info->init_array_addr), n);
43 
44  for (auto i = 0U; i < n && init_array.at(i) != nullptr; i++)
45  init_array.at(i)();
46  }
47  }
48  catch (...)
49  {
50  return CRT_FAILURE;
51  }
52 
53  auto ret = register_eh_frame(info->eh_frame_addr, info->eh_frame_size);
54  if (ret != REGISTER_EH_FRAME_SUCCESS)
55  return ret;
56 
57  return CRT_SUCCESS;
58 }
59 
60 extern "C" int64_t
62 {
63  if (info == nullptr)
64  return CRT_FAILURE;
65 
66  try
67  {
68  if (info->fini_array_addr != nullptr)
69  {
70  auto n = static_cast<std::ptrdiff_t>(info->fini_array_size >> 3);
71  auto fini_array = gsl::make_span(static_cast<fini_t *>(info->fini_array_addr), n);
72 
73  for (auto i = 0U; i < n && fini_array.at(i) != nullptr; i++)
74  fini_array.at(i)();
75  }
76 
77  if (info->fini_addr != nullptr)
78  reinterpret_cast<fini_t>(info->fini_addr)();
79  }
80  catch (...)
81  {
82  return CRT_FAILURE;
83  }
84 
85  return CRT_SUCCESS;
86 }
void * eh_frame_addr
Definition: crt.h:118
void(* fini_t)()
Definition: crt.cpp:26
uint64_t fini_array_size
Definition: crt.h:116
void * init_addr
Definition: crt.h:109
void * init_array_addr
Definition: crt.h:112
void * fini_addr
Definition: crt.h:110
void * fini_array_addr
Definition: crt.h:115
uint64_t init_array_size
Definition: crt.h:113
int64_t local_fini(struct section_info_t *info)
Definition: crt.cpp:61
int64_t local_init(struct section_info_t *info)
Definition: crt.cpp:29
#define CRT_FAILURE
Definition: error_codes.h:60
int64_t register_eh_frame(void *addr, uint64_t size)
Definition: dummy_misc.cpp:62
#define CRT_SUCCESS
Definition: error_codes.h:59
uint64_t eh_frame_size
Definition: crt.h:119
#define REGISTER_EH_FRAME_SUCCESS
Definition: error_codes.h:66
void(* init_t)()
Definition: crt.cpp:25