summaryrefslogtreecommitdiff
path: root/ACE/apps/soreduce/Obj_Module.cpp
blob: 75fe1ce38a5b67b2dd8ae24814d1ad98a2d2ea58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// -*- C++ -*-
// File: Obj_Module.cpp

// Author: Phil Mesnier

// This file contains the implementation of the classes responsible for
// managing the contents of a single object module (.o file).

#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Process.h"
#include "ace/Pipe.h"
#include "ace/Message_Block.h"
#include "ace/Log_Msg.h"

#include "Obj_Module.h"

//----------------------------------------------------------------------------

Obj_Module::Obj_Module (const ACE_CString &name, int cap)
  : name_ (name),
    imports_(cap),
    exports_(cap),
    extrefs_(0)
{
}

ACE_CString &
Obj_Module::name()
{
  return name_;
}

Sig_List &
Obj_Module::exports()
{
  return exports_;
}

Sig_List &
Obj_Module::imports()
{
  return imports_;
}

int
Obj_Module::extref()
{
  return extrefs_;
}

void
Obj_Module::add_extref()
{
  extrefs_ ++;
}

void
Obj_Module::remove_extref()
{
  extrefs_ --;
}

int
Obj_Module::read_line (ACE_HANDLE src, ACE_Message_Block **buf)
{
  int eoln = 0;
  //  ACE_Time_Value timeout (1,0);
  if (buf == 0 || *buf == 0) {
    char dummy;
    while (!eoln && ACE_OS::read(src,&dummy,1) == 1) {
      eoln = (dummy == '\n');
    }
    return eoln;
  }
  //  while (!eoln && ACE::recv(src,buf->wr_ptr(),1,&timeout) == 1) {
  while (!eoln && ACE_OS::read(src,(*buf)->wr_ptr(),1) == 1) {
    eoln = (*(*buf)->wr_ptr() == '\n');
    (*buf)->wr_ptr(1);
    if ((*buf)->space() == 0) {
      (*buf)->cont(new ACE_Message_Block(102400));
      *buf = (*buf)->cont();
    }
  }
  return eoln;
}

void
Obj_Module::add_source(const char *p, int imports_only)
{
  ACE_Process nmproc;
  ACE_Process_Options nm_opts;
  ACE_CString path (p);

  ACE_CString::size_type pathsep = path.rfind('/');

  ACE_CString src_name;
  ACE_CString workpath;

  if (pathsep == ACE_CString::npos) {
    src_name = path;
    workpath = ".";
  } else {
    src_name = path.substr(pathsep+1);
    workpath= path.substr(0,pathsep);
  }

  ACE_HANDLE pipe[2];
  ACE_Pipe io(pipe);

  nm_opts.working_directory (workpath.c_str());
  nm_opts.set_handles (ACE_STDIN,pipe[1]);

  // Options for the command line shown here are for the GNU nm 2.9.5

  int result = nm_opts.command_line ("nm -C %s",src_name.c_str());
  // Prevent compiler warning about "unused variable" if ACE_ASSERT is
  // an empty macro.
  ACE_UNUSED_ARG (result);
  ACE_ASSERT (result == 0);

  nmproc.spawn (nm_opts);
  if (ACE_OS::close(pipe[1]) == -1)
    ACE_DEBUG ((LM_DEBUG, "%p\n", "close"));
  nm_opts.release_handles();

  int import_lines = 0;
  int export_lines = 0;
  ACE_Message_Block im_buffer (102400);
  ACE_Message_Block ex_buffer (102400);
  ACE_Message_Block *im_buf_cur = &im_buffer;
  ACE_Message_Block *ex_buf_cur = &ex_buffer;
  char dummy;
  int eoln = 1;
  //  ACE_Time_Value timeout (1,0);
  int is_import = 1;
  int is_export = 1;

  while (eoln == 1) {
    for (int i = 0; i < 10; i++) {
      if (ACE_OS::read(pipe[0],&dummy,1) != 1) {
        eoln = 2;
        break;
      }
    }
    if (eoln == 2)
      break;
    is_import = dummy == 'U';
    is_export = !imports_only && (ACE_OS::strchr("BCDRTVW",dummy) != 0);

    //    if (ACE::recv(pipe[0],&dummy,1,&timeout) != 1)
    if (ACE_OS::read(pipe[0],&dummy,1) != 1)
      break;

    eoln = this->read_line (pipe[0], is_import ? &im_buf_cur :
                            (is_export ? &ex_buf_cur : 0));
    import_lines += is_import;
    export_lines += is_export;
  }
  //  ACE_DEBUG ((LM_DEBUG, "read %d import lines and %d export lines\n",
  //            import_lines, export_lines));

  nmproc.wait ();
  ACE_OS::close (pipe[0]);

  this->populate_sig_list (imports_,import_lines,&im_buffer);
  if (!imports_only)
    this->populate_sig_list (exports_,export_lines,&ex_buffer);
}

void
Obj_Module::populate_sig_list (Sig_List &siglist,
                               int lines,
                               ACE_Message_Block *buf)
{
  char *c;
  ACE_CString temp;

  for (int i = 0; i < lines; i++)
    {
      for (c = buf->rd_ptr (); c != buf->wr_ptr () && *c != '\n'; ++c)
        {
          // No action.
        }

      temp += ACE_CString (buf->rd_ptr (), (c - buf->rd_ptr ()));
      buf->rd_ptr (c + 1);

      if (*c == '\n')
        {
          //      ACE_DEBUG ((LM_DEBUG, "%s\n",temp.c_str()));
          siglist.add (temp);
          temp.clear ();
        }
      else
        {
          buf = buf->cont ();

          if (buf == 0)
            {
              siglist.add (temp);
            }
        }
    }
}