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 void GetIRTInterfaces() { |
73 void GetIRTInterface() { | |
74 size_t query_result = nacl_interface_query( | 71 size_t query_result = nacl_interface_query( |
75 NACL_IRT_RESOURCE_OPEN_v0_1, | 72 NACL_IRT_RESOURCE_OPEN_v0_1, |
76 &g_irt_resource_open, sizeof(g_irt_resource_open)); | 73 &g_irt_resource_open, sizeof(g_irt_resource_open)); |
77 if (query_result != sizeof(g_irt_resource_open)) { | 74 if (query_result != sizeof(g_irt_resource_open)) { |
78 gold_fatal(_("nacl_file::GetIRTInterface failed")); | 75 gold_fatal(_("Failed to get resource_open IRT interface")); |
| 76 } |
| 77 |
| 78 query_result = nacl_interface_query( |
| 79 NACL_IRT_PRIVATE_PNACL_TRANSLATOR_LINK_v0_1, |
| 80 &g_irt_translator_link, sizeof(g_irt_translator_link)); |
| 81 if (query_result != sizeof(g_irt_translator_link)) { |
| 82 gold_fatal(_("Failed to get translator_link IRT interface")); |
79 } | 83 } |
80 } | 84 } |
81 | 85 |
82 int IrtOpenFile(const char* filename) { | 86 int IrtOpenFile(const char* filename) { |
83 int fd = -1; | 87 int fd = -1; |
84 if (int res = g_irt_resource_open.open_resource(filename, &fd)) { | 88 if (int res = g_irt_resource_open.open_resource(filename, &fd)) { |
85 gold_fatal(_("IrtOpenFile (%s) failed: %d\n"), filename, res); | 89 gold_fatal(_("IrtOpenFile (%s) failed: %d\n"), filename, res); |
86 } | 90 } |
87 return fd; | 91 return fd; |
88 } | 92 } |
89 | 93 |
90 void RunCommon(const std::vector<std::string>& arg_vec, | 94 int RunCommon(const std::vector<std::string>& arg_vec) { |
91 NaClSrpcRpc* rpc, | |
92 NaClSrpcClosure* done) { | |
93 // repackage the commandline to what main() expects | 95 // repackage the commandline to what main() expects |
94 const char* argv[kMaxArgc]; | 96 const char* argv[kMaxArgc]; |
95 if (arg_vec.size() > kMaxArgc) { | 97 if (arg_vec.size() > kMaxArgc) { |
96 gold_fatal(_("commandline too long")); | 98 gold_fatal(_("commandline too long")); |
97 } | 99 } |
98 for (size_t i = 0; i < arg_vec.size(); ++i) argv[i] = arg_vec[i].c_str(); | 100 for (size_t i = 0; i < arg_vec.size(); ++i) argv[i] = arg_vec[i].c_str(); |
99 | 101 |
100 // call hijacked main() | 102 // call hijacked main() |
101 int ret = gold_main(arg_vec.size(), const_cast<char**>(&argv[0])); | 103 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 } | 104 } |
105 | 105 |
106 // c.f.: pnacl/driver/nativeld.py | 106 // c.f.: pnacl/driver/nativeld.py |
107 const char* kDefaultCommandCommon[] = { | 107 const char* kDefaultCommandCommon[] = { |
108 "gold", | 108 "gold", |
109 "--eh-frame-hdr", | 109 "--eh-frame-hdr", |
110 "-nostdlib", | 110 "-nostdlib", |
111 // ARM only but added to everything for convenience | 111 // ARM only but added to everything for convenience |
112 "--no-fix-cortex-a8", | 112 "--no-fix-cortex-a8", |
113 "-o", | 113 "-o", |
(...skipping 26 matching lines...) Expand all Loading... |
140 if (src[i] == std::string("@shim")) { | 140 if (src[i] == std::string("@shim")) { |
141 result->push_back("--entry=__pnacl_start"); | 141 result->push_back("--entry=__pnacl_start"); |
142 result->push_back("libpnacl_irt_shim.a"); | 142 result->push_back("libpnacl_irt_shim.a"); |
143 } else { | 143 } else { |
144 gold_fatal(_("unknown meta command line flag")); | 144 gold_fatal(_("unknown meta command line flag")); |
145 } | 145 } |
146 } | 146 } |
147 } | 147 } |
148 | 148 |
149 | 149 |
150 // SRPC signature: :ihhhhhhhhhhhhhhhhh: | 150 int HandleLinkRequest(int nexe_fd, |
151 // i: number N of object files to use | 151 const int* obj_file_fds, |
152 // h{16}: handles of objfiles. N of them are valid. | 152 int object_count) { |
153 // h: handle of nexe file | 153 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")); | 154 gold_fatal(_("Invalid object count")); |
162 } | 155 } |
163 std::vector<char *> filenames(object_count); | 156 std::vector<char *> filenames(object_count); |
164 for (int i = 0; i < object_count; i++) { | 157 for (int i = 0; i < object_count; i++) { |
165 const int len = sizeof(FILENAME_OBJ) + 2; | 158 const int len = sizeof(FILENAME_OBJ) + 2; |
166 filenames[i] = new char[len]; | 159 filenames[i] = new char[len]; |
167 snprintf(filenames[i], len, "%s%d", FILENAME_OBJ, i); | 160 snprintf(filenames[i], len, "%s%d", FILENAME_OBJ, i); |
168 RegisterPreopenedFd(filenames[i], in_args[i + 1]->u.hval); | 161 RegisterPreopenedFd(filenames[i], obj_file_fds[i]); |
169 } | 162 } |
170 int nexe_fd = in_args[kMaxObjectFiles + 1]->u.hval; | |
171 RegisterPreopenedFd(FILENAME_OUTPUT, nexe_fd); | 163 RegisterPreopenedFd(FILENAME_OUTPUT, nexe_fd); |
172 | 164 |
173 std::vector<std::string> result_arg_vec; | 165 std::vector<std::string> result_arg_vec; |
174 | 166 |
175 ProcessCommandline(kDefaultCommandCommon, &result_arg_vec); | 167 ProcessCommandline(kDefaultCommandCommon, &result_arg_vec); |
176 // Construct the rest of the command line, replacing FILENAME_OBJ with a list | 168 // Construct the rest of the command line, replacing FILENAME_OBJ with a list |
177 // of input files from the descriptors. | 169 // of input files from the descriptors. |
178 const int cmdline_len = ((sizeof(kDefaultCommandStatic) / | 170 const int cmdline_len = ((sizeof(kDefaultCommandStatic) / |
179 sizeof(kDefaultCommandStatic[0])) + | 171 sizeof(kDefaultCommandStatic[0])) + |
180 object_count - 1); | 172 object_count - 1); |
181 const char *command_line[cmdline_len]; | 173 const char *command_line[cmdline_len]; |
182 | 174 |
183 int arg = 0; | 175 int arg = 0; |
184 for (int i = 0; kDefaultCommandStatic[i] != 0; i++) { | 176 for (int i = 0; kDefaultCommandStatic[i] != 0; i++) { |
185 if (!strcmp(kDefaultCommandStatic[i], FILENAME_OBJ)) { | 177 if (!strcmp(kDefaultCommandStatic[i], FILENAME_OBJ)) { |
186 for (int k = 0; k < object_count; k++) { | 178 for (int k = 0; k < object_count; k++) { |
187 command_line[arg++] = filenames[k]; | 179 command_line[arg++] = filenames[k]; |
188 } | 180 } |
189 } else { | 181 } else { |
190 command_line[arg++] = kDefaultCommandStatic[i]; | 182 command_line[arg++] = kDefaultCommandStatic[i]; |
191 } | 183 } |
192 } | 184 } |
193 command_line[arg] = 0; | 185 command_line[arg] = 0; |
194 | 186 |
195 ProcessCommandline(command_line, &result_arg_vec); | 187 ProcessCommandline(command_line, &result_arg_vec); |
196 for (int i = 0; i < object_count; i++) { | 188 for (int i = 0; i < object_count; i++) { |
197 delete [] filenames[i]; | 189 delete [] filenames[i]; |
198 } | 190 } |
199 RunCommon(result_arg_vec, rpc, done); | 191 return RunCommon(result_arg_vec); |
200 } | 192 } |
201 | 193 |
202 } // End anonymous namespace. | 194 } // End anonymous namespace. |
203 | 195 |
204 namespace nacl_file | 196 namespace nacl_file |
205 { | 197 { |
206 | 198 |
207 int NaClOpenFileDescriptor(const char *filename) { | 199 int NaClOpenFileDescriptor(const char *filename) { |
208 std::string key(filename); | 200 std::string key(filename); |
209 std::map<std::string, int>::iterator it = g_preopened_files.find(key); | 201 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. | 217 // Note: we do not close the fd as it maybe opened again. |
226 // For now we are getting lucky: | 218 // For now we are getting lucky: |
227 // gold is not closing any of the libraries. And it IS closing | 219 // gold is not closing any of the libraries. And it IS closing |
228 // the nexe for us in Output_file::close | 220 // the nexe for us in Output_file::close |
229 } | 221 } |
230 | 222 |
231 } // End namespace nacl_file. | 223 } // End namespace nacl_file. |
232 | 224 |
233 | 225 |
234 int main() { | 226 int main() { |
235 if (!NaClSrpcModuleInit()) { | 227 GetIRTInterfaces(); |
236 gold_fatal(_("NaClSrpcModuleInit failed\n")); | |
237 } | |
238 GetIRTInterface(); | |
239 | 228 |
240 // Start the message loop to process SRPCs. | 229 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 | 230 |
247 if (!NaClSrpcAcceptClientConnection(srpc_methods)) { | |
248 gold_fatal(_("NaClSrpcAcceptClientConnection failed\n")); | |
249 } | |
250 | |
251 NaClSrpcModuleFini(); | |
252 return 0; | 231 return 0; |
253 } | 232 } |
OLD | NEW |