OLD | NEW |
---|---|
1 // nacl_file.cc -- handle file lookups for NaCl version of gold -*- C++ -*- | 1 // nacl_file.cc -- handle file lookups for NaCl version of gold -*- C++ -*- |
2 | 2 |
3 // Copyright 2012 Free Software Foundation, Inc. | 3 // Copyright 2012 Free Software Foundation, Inc. |
4 // Written by the Native Client authors. | 4 // Written by the Native Client authors. |
5 | 5 |
6 // This file is part of gold. | 6 // This file is part of gold. |
7 | 7 |
8 // This program is free software; you can redistribute it and/or modify | 8 // This program is free software; you can redistribute it and/or modify |
9 // it under the terms of the GNU General Public License as published by | 9 // it under the terms of the GNU General Public License as published by |
10 // the Free Software Foundation; either version 3 of the License, or | 10 // the Free Software Foundation; either version 3 of the License, or |
(...skipping 16 matching lines...) Expand all Loading... | |
27 #error "Cannot build nacl_file.cc without __native_client__" | 27 #error "Cannot build nacl_file.cc without __native_client__" |
28 #endif | 28 #endif |
29 | 29 |
30 #include <fcntl.h> | 30 #include <fcntl.h> |
31 #include <map> | 31 #include <map> |
32 #include <string> | 32 #include <string> |
33 #include <unistd.h> | 33 #include <unistd.h> |
34 #include <vector> | 34 #include <vector> |
35 | 35 |
36 #include <irt.h> | 36 #include <irt.h> |
37 #include "native_client/src/shared/srpc/nacl_srpc.h" | 37 #include <irt_dev.h> |
38 | 38 |
39 #include "gold.h" | 39 #include "gold.h" |
40 | 40 |
41 using namespace gold; | 41 using namespace gold; |
42 | 42 |
43 extern int gold_main(int argc, char** argv); | 43 extern int gold_main(int argc, char** argv); |
44 | 44 |
45 #define FILENAME_OUTPUT "a.out" | 45 #define FILENAME_OUTPUT "a.out" |
46 #define FILENAME_OBJ "__PNACL_GENERATED" | 46 #define FILENAME_OBJ "__PNACL_GENERATED" |
47 | 47 |
48 const int kMaxArgc = 256; | 48 const int kMaxArgc = 256; |
49 // This value cannot change without also changing the signature of the | |
50 // RunWithSplit RPC | |
51 const int kMaxObjectFiles = 16; | |
52 | 49 |
53 namespace | 50 namespace |
54 { | 51 { |
55 std::map<std::string, int> g_preopened_files; | 52 std::map<std::string, int> g_preopened_files; |
56 struct nacl_irt_resource_open g_irt_resource_open; | 53 struct nacl_irt_resource_open g_irt_resource_open; |
54 struct nacl_irt_private_pnacl_translator_link g_irt_translator_link; | |
57 | 55 |
58 // Register some filename -> fd mappings that correspond to pre-opened fds. | 56 // Register some filename -> fd mappings that correspond to pre-opened fds. |
59 // Otherwise files are opened via the IRT open_resource() function. | 57 // Otherwise files are opened via the IRT open_resource() function. |
60 void RegisterPreopenedFd(const char* filename, int fd) { | 58 void RegisterPreopenedFd(const char* filename, int fd) { |
61 std::string key(filename); | 59 std::string key(filename); |
62 std::map<std::string, int>::iterator it = g_preopened_files.find(key); | 60 std::map<std::string, int>::iterator it = g_preopened_files.find(key); |
63 | 61 |
64 if (it != g_preopened_files.end()) { | 62 if (it != g_preopened_files.end()) { |
65 gold_fatal(_("nacl_file::set_preopened_fd already set %s to %d."), | 63 gold_fatal(_("nacl_file::set_preopened_fd already set %s to %d."), |
66 filename, it->second); | 64 filename, it->second); |
67 } else { | 65 } else { |
68 g_preopened_files[key] = fd; | 66 g_preopened_files[key] = fd; |
69 } | 67 } |
70 } | 68 } |
71 | 69 |
72 // Set up interfaces for IRT open_resource. | 70 // Set up interfaces for IRT open_resource. |
jvoung (off chromium)
2015/02/05 19:11:25
nit: Does a bit more than "for IRT open_resource"
Mark Seaborn
2015/02/05 23:19:45
Good point. I'll remove the comment, since the fu
| |
73 void GetIRTInterface() { | 71 void GetIRTInterface() { |
74 size_t query_result = nacl_interface_query( | 72 size_t query_result = nacl_interface_query( |
75 NACL_IRT_RESOURCE_OPEN_v0_1, | 73 NACL_IRT_RESOURCE_OPEN_v0_1, |
76 &g_irt_resource_open, sizeof(g_irt_resource_open)); | 74 &g_irt_resource_open, sizeof(g_irt_resource_open)); |
77 if (query_result != sizeof(g_irt_resource_open)) { | 75 if (query_result != sizeof(g_irt_resource_open)) { |
78 gold_fatal(_("nacl_file::GetIRTInterface failed")); | 76 gold_fatal(_("Failed to get resource_open IRT interface")); |
77 } | |
78 | |
79 query_result = nacl_interface_query( | |
80 NACL_IRT_PRIVATE_PNACL_TRANSLATOR_LINK_v0_1, | |
81 &g_irt_translator_link, sizeof(g_irt_translator_link)); | |
82 if (query_result != sizeof(g_irt_translator_link)) { | |
83 gold_fatal(_("Failed to get translator_link IRT interface")); | |
79 } | 84 } |
80 } | 85 } |
81 | 86 |
82 int IrtOpenFile(const char* filename) { | 87 int IrtOpenFile(const char* filename) { |
83 int fd = -1; | 88 int fd = -1; |
84 if (int res = g_irt_resource_open.open_resource(filename, &fd)) { | 89 if (int res = g_irt_resource_open.open_resource(filename, &fd)) { |
85 gold_fatal(_("IrtOpenFile (%s) failed: %d\n"), filename, res); | 90 gold_fatal(_("IrtOpenFile (%s) failed: %d\n"), filename, res); |
86 } | 91 } |
87 return fd; | 92 return fd; |
88 } | 93 } |
89 | 94 |
90 void RunCommon(const std::vector<std::string>& arg_vec, | 95 int RunCommon(const std::vector<std::string>& arg_vec) { |
91 NaClSrpcRpc* rpc, | |
92 NaClSrpcClosure* done) { | |
93 // repackage the commandline to what main() expects | 96 // repackage the commandline to what main() expects |
94 const char* argv[kMaxArgc]; | 97 const char* argv[kMaxArgc]; |
95 if (arg_vec.size() > kMaxArgc) { | 98 if (arg_vec.size() > kMaxArgc) { |
96 gold_fatal(_("commandline too long")); | 99 gold_fatal(_("commandline too long")); |
97 } | 100 } |
98 for (size_t i = 0; i < arg_vec.size(); ++i) argv[i] = arg_vec[i].c_str(); | 101 for (size_t i = 0; i < arg_vec.size(); ++i) argv[i] = arg_vec[i].c_str(); |
99 | 102 |
100 // call hijacked main() | 103 // call hijacked main() |
101 int ret = gold_main(arg_vec.size(), const_cast<char**>(&argv[0])); | 104 return gold_main(arg_vec.size(), const_cast<char**>(&argv[0])); |
102 rpc->result = ret > 0 ? NACL_SRPC_RESULT_APP_ERROR : NACL_SRPC_RESULT_OK; | |
103 done->Run(done); | |
104 } | 105 } |
105 | 106 |
106 // c.f.: pnacl/driver/nativeld.py | 107 // c.f.: pnacl/driver/nativeld.py |
107 const char* kDefaultCommandCommon[] = { | 108 const char* kDefaultCommandCommon[] = { |
108 "gold", | 109 "gold", |
109 "--eh-frame-hdr", | 110 "--eh-frame-hdr", |
110 "-nostdlib", | 111 "-nostdlib", |
111 // ARM only but added to everything for convenience | 112 // ARM only but added to everything for convenience |
112 "--no-fix-cortex-a8", | 113 "--no-fix-cortex-a8", |
113 "-o", | 114 "-o", |
(...skipping 26 matching lines...) Expand all Loading... | |
140 if (src[i] == std::string("@shim")) { | 141 if (src[i] == std::string("@shim")) { |
141 result->push_back("--entry=__pnacl_start"); | 142 result->push_back("--entry=__pnacl_start"); |
142 result->push_back("libpnacl_irt_shim.a"); | 143 result->push_back("libpnacl_irt_shim.a"); |
143 } else { | 144 } else { |
144 gold_fatal(_("unknown meta command line flag")); | 145 gold_fatal(_("unknown meta command line flag")); |
145 } | 146 } |
146 } | 147 } |
147 } | 148 } |
148 | 149 |
149 | 150 |
150 // SRPC signature: :ihhhhhhhhhhhhhhhhh: | 151 int HandleLinkRequest(int nexe_fd, |
151 // i: number N of object files to use | 152 const int* obj_file_fds, |
152 // h{16}: handles of objfiles. N of them are valid. | 153 int object_count) { |
153 // h: handle of nexe file | 154 if (object_count < 1) { |
154 void | |
155 SrpcRunWithSplit(NaClSrpcRpc* rpc, | |
156 NaClSrpcArg** in_args, | |
157 NaClSrpcArg** /* out_args */, | |
158 NaClSrpcClosure* done) { | |
159 int object_count = in_args[0]->u.ival; | |
160 if (object_count > kMaxObjectFiles || object_count < 1) { | |
161 gold_fatal(_("Invalid object count")); | 155 gold_fatal(_("Invalid object count")); |
162 } | 156 } |
163 std::vector<char *> filenames(object_count); | 157 std::vector<char *> filenames(object_count); |
164 for (int i = 0; i < object_count; i++) { | 158 for (int i = 0; i < object_count; i++) { |
165 const int len = sizeof(FILENAME_OBJ) + 2; | 159 const int len = sizeof(FILENAME_OBJ) + 2; |
166 filenames[i] = new char[len]; | 160 filenames[i] = new char[len]; |
167 snprintf(filenames[i], len, "%s%d", FILENAME_OBJ, i); | 161 snprintf(filenames[i], len, "%s%d", FILENAME_OBJ, i); |
168 RegisterPreopenedFd(filenames[i], in_args[i + 1]->u.hval); | 162 RegisterPreopenedFd(filenames[i], obj_file_fds[i]); |
169 } | 163 } |
170 int nexe_fd = in_args[kMaxObjectFiles + 1]->u.hval; | |
171 RegisterPreopenedFd(FILENAME_OUTPUT, nexe_fd); | 164 RegisterPreopenedFd(FILENAME_OUTPUT, nexe_fd); |
172 | 165 |
173 std::vector<std::string> result_arg_vec; | 166 std::vector<std::string> result_arg_vec; |
174 | 167 |
175 ProcessCommandline(kDefaultCommandCommon, &result_arg_vec); | 168 ProcessCommandline(kDefaultCommandCommon, &result_arg_vec); |
176 // Construct the rest of the command line, replacing FILENAME_OBJ with a list | 169 // Construct the rest of the command line, replacing FILENAME_OBJ with a list |
177 // of input files from the descriptors. | 170 // of input files from the descriptors. |
178 const int cmdline_len = ((sizeof(kDefaultCommandStatic) / | 171 const int cmdline_len = ((sizeof(kDefaultCommandStatic) / |
179 sizeof(kDefaultCommandStatic[0])) + | 172 sizeof(kDefaultCommandStatic[0])) + |
180 object_count - 1); | 173 object_count - 1); |
181 const char *command_line[cmdline_len]; | 174 const char *command_line[cmdline_len]; |
182 | 175 |
183 int arg = 0; | 176 int arg = 0; |
184 for (int i = 0; kDefaultCommandStatic[i] != 0; i++) { | 177 for (int i = 0; kDefaultCommandStatic[i] != 0; i++) { |
185 if (!strcmp(kDefaultCommandStatic[i], FILENAME_OBJ)) { | 178 if (!strcmp(kDefaultCommandStatic[i], FILENAME_OBJ)) { |
186 for (int k = 0; k < object_count; k++) { | 179 for (int k = 0; k < object_count; k++) { |
187 command_line[arg++] = filenames[k]; | 180 command_line[arg++] = filenames[k]; |
188 } | 181 } |
189 } else { | 182 } else { |
190 command_line[arg++] = kDefaultCommandStatic[i]; | 183 command_line[arg++] = kDefaultCommandStatic[i]; |
191 } | 184 } |
192 } | 185 } |
193 command_line[arg] = 0; | 186 command_line[arg] = 0; |
194 | 187 |
195 ProcessCommandline(command_line, &result_arg_vec); | 188 ProcessCommandline(command_line, &result_arg_vec); |
196 for (int i = 0; i < object_count; i++) { | 189 for (int i = 0; i < object_count; i++) { |
197 delete [] filenames[i]; | 190 delete [] filenames[i]; |
198 } | 191 } |
199 RunCommon(result_arg_vec, rpc, done); | 192 return RunCommon(result_arg_vec); |
200 } | 193 } |
201 | 194 |
202 } // End anonymous namespace. | 195 } // End anonymous namespace. |
203 | 196 |
204 namespace nacl_file | 197 namespace nacl_file |
205 { | 198 { |
206 | 199 |
207 int NaClOpenFileDescriptor(const char *filename) { | 200 int NaClOpenFileDescriptor(const char *filename) { |
208 std::string key(filename); | 201 std::string key(filename); |
209 std::map<std::string, int>::iterator it = g_preopened_files.find(key); | 202 std::map<std::string, int>::iterator it = g_preopened_files.find(key); |
(...skipping 15 matching lines...) Expand all Loading... | |
225 // Note: we do not close the fd as it maybe opened again. | 218 // Note: we do not close the fd as it maybe opened again. |
226 // For now we are getting lucky: | 219 // For now we are getting lucky: |
227 // gold is not closing any of the libraries. And it IS closing | 220 // gold is not closing any of the libraries. And it IS closing |
228 // the nexe for us in Output_file::close | 221 // the nexe for us in Output_file::close |
229 } | 222 } |
230 | 223 |
231 } // End namespace nacl_file. | 224 } // End namespace nacl_file. |
232 | 225 |
233 | 226 |
234 int main() { | 227 int main() { |
235 if (!NaClSrpcModuleInit()) { | |
236 gold_fatal(_("NaClSrpcModuleInit failed\n")); | |
237 } | |
238 GetIRTInterface(); | 228 GetIRTInterface(); |
239 | 229 |
240 // Start the message loop to process SRPCs. | 230 g_irt_translator_link.serve_link_request(HandleLinkRequest); |
241 // It usually never terminates unless killed. | |
242 const struct NaClSrpcHandlerDesc srpc_methods[] = { | |
243 { "RunWithSplit:ihhhhhhhhhhhhhhhhh:", SrpcRunWithSplit }, | |
244 { NULL, NULL }, | |
245 }; | |
246 | 231 |
247 if (!NaClSrpcAcceptClientConnection(srpc_methods)) { | |
248 gold_fatal(_("NaClSrpcAcceptClientConnection failed\n")); | |
249 } | |
250 | |
251 NaClSrpcModuleFini(); | |
252 return 0; | 232 return 0; |
253 } | 233 } |
OLD | NEW |