2 // File: Obj_Module.cpp
4 // Author: Phil Mesnier
6 // This file contains the implementation of the classes responsible for
7 // managing the contents of a single object module (.o file).
9 #include "ace/OS_NS_string.h"
10 #include "ace/OS_NS_unistd.h"
11 #include "ace/Process.h"
13 #include "ace/Message_Block.h"
14 #include "ace/Log_Msg.h"
16 #include "Obj_Module.h"
18 //----------------------------------------------------------------------------
20 Obj_Module::Obj_Module (const ACE_CString
&name
, int cap
)
53 Obj_Module::add_extref()
59 Obj_Module::remove_extref()
65 Obj_Module::read_line (ACE_HANDLE src
, ACE_Message_Block
**buf
)
68 // ACE_Time_Value timeout (1,0);
69 if (buf
== 0 || *buf
== 0) {
71 while (!eoln
&& ACE_OS::read(src
,&dummy
,1) == 1) {
72 eoln
= (dummy
== '\n');
76 // while (!eoln && ACE::recv(src,buf->wr_ptr(),1,&timeout) == 1) {
77 while (!eoln
&& ACE_OS::read(src
,(*buf
)->wr_ptr(),1) == 1) {
78 eoln
= (*(*buf
)->wr_ptr() == '\n');
80 if ((*buf
)->space() == 0) {
81 (*buf
)->cont(new ACE_Message_Block(102400));
82 *buf
= (*buf
)->cont();
89 Obj_Module::add_source(const char *p
, int imports_only
)
92 ACE_Process_Options nm_opts
;
95 ACE_CString::size_type pathsep
= path
.rfind('/');
100 if (pathsep
== ACE_CString::npos
) {
104 src_name
= path
.substr(pathsep
+1);
105 workpath
= path
.substr(0,pathsep
);
111 nm_opts
.working_directory (workpath
.c_str());
112 nm_opts
.set_handles (ACE_STDIN
,pipe
[1]);
114 // Options for the command line shown here are for the GNU nm 2.9.5
116 int result
= nm_opts
.command_line ("nm -C %s",src_name
.c_str());
117 // Prevent compiler warning about "unused variable" if ACE_ASSERT is
119 ACE_UNUSED_ARG (result
);
120 ACE_ASSERT (result
== 0);
122 nmproc
.spawn (nm_opts
);
123 if (ACE_OS::close(pipe
[1]) == -1)
124 ACE_DEBUG ((LM_DEBUG
, "%p\n", "close"));
125 nm_opts
.release_handles();
127 int import_lines
= 0;
128 int export_lines
= 0;
129 ACE_Message_Block
im_buffer (102400);
130 ACE_Message_Block
ex_buffer (102400);
131 ACE_Message_Block
*im_buf_cur
= &im_buffer
;
132 ACE_Message_Block
*ex_buf_cur
= &ex_buffer
;
135 // ACE_Time_Value timeout (1,0);
140 for (int i
= 0; i
< 10; i
++) {
141 if (ACE_OS::read(pipe
[0],&dummy
,1) != 1) {
148 is_import
= dummy
== 'U';
149 is_export
= !imports_only
&& (ACE_OS::strchr("BCDRTVW",dummy
) != 0);
151 // if (ACE::recv(pipe[0],&dummy,1,&timeout) != 1)
152 if (ACE_OS::read(pipe
[0],&dummy
,1) != 1)
155 eoln
= this->read_line (pipe
[0], is_import
? &im_buf_cur
:
156 (is_export
? &ex_buf_cur
: 0));
157 import_lines
+= is_import
;
158 export_lines
+= is_export
;
160 // ACE_DEBUG ((LM_DEBUG, "read %d import lines and %d export lines\n",
161 // import_lines, export_lines));
164 ACE_OS::close (pipe
[0]);
166 this->populate_sig_list (imports_
,import_lines
,&im_buffer
);
168 this->populate_sig_list (exports_
,export_lines
,&ex_buffer
);
172 Obj_Module::populate_sig_list (Sig_List
&siglist
,
174 ACE_Message_Block
*buf
)
179 for (int i
= 0; i
< lines
; i
++)
181 for (c
= buf
->rd_ptr (); c
!= buf
->wr_ptr () && *c
!= '\n'; ++c
)
186 temp
+= ACE_CString (buf
->rd_ptr (), (c
- buf
->rd_ptr ()));
191 // ACE_DEBUG ((LM_DEBUG, "%s\n",temp.c_str()));