This file is indexed.

/usr/bin/tosthreads-dynamic-app is in tinyos-tools 1.4.2-3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/python2

# Copyright (c) 2008 Johns Hopkins University.
# All rights reserved.
#
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions
 # are met:
 #
 # - Redistributions of source code must retain the above copyright
 #   notice, this list of conditions and the following disclaimer.
 # - Redistributions in binary form must reproduce the above copyright
 #   notice, this list of conditions and the following disclaimer in the
 #   documentation and/or other materials provided with the
 #   distribution.
 # - Neither the name of the copyright holders nor the names of
 #   its contributors may be used to endorse or promote products derived
 #   from this software without specific prior written permission.
 #
 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 # OF THE POSSIBILITY OF SUCH DAMAGE.

# @author Chieh-Jan Mike Liang <cliang4@cs.jhu.edu>
# @author Razvan Musaloiu-E. <razvanm@cs.jhu.edu>
# @author Kevin Klues <klueska@cs.stanford.edu>

import sys, subprocess
import struct
sys.path.append("/usr/lib/x86_64-linux-gnu/tinyos/tosthreads")

def error_exit( s ):
    sys.stderr.write("\n" + s + "\n\n")
    exit( 2 )
    
def exit_usage():
  error_exit( "Usage: tosthreads-dynamic-app [-a --array --api=<standard, tenet> ] <obj_file> <bin_file> <tos_file>" )

#Handle arguments........
from getopt import *
try:
  opts, args = getopt(sys.argv[1:], "a" ,['array', 'api='])
except GetoptError, err:
  print str(err) # will print something like "option -a not recognized"
  exit_usage()

array_opt = False
api_opt = False
valid_apis = ["standard", "tenet"];
for o, a in opts:
  if o == "--api" and (a not in valid_apis):
    exit_usage()
  elif o == "--api" and a == "tenet":
    api_opt = True
    import tosthreads_tenet_api as tosthread_slcs_extfun
  elif o == "--api" and a == "standard":
    api_opt = True
    import tosthreads_standard_api as tosthread_slcs_extfun
  elif o == "-a" or o == "--array":
    array_opt = True
  else:
    exit_usage()

if api_opt == False:
  import tosthreads_standard_api as tosthread_slcs_extfun

if len( args ) != 3:
  exit_usage()

def slice(v, s):
    r = []
    for i in range(0, len(v), s):
        r.append(v[i:i+s])
    return r

def cmp(x, y):
    if int(x[0]) > int(y[0]):
      return 1
    elif int(x[0]) == int(y[0]):
      if x[1] > y[1]:
        return 1
      elif x[1] == y[1]:
        return 0
      else:
        return -1
    else:
      return -1

# ===== STEP 0: Prepares function-ID maps ===== #
map_extfun = tosthread_slcs_extfun.map_extfun

map_hook = {"tosthread_main":0}
map_intfun = dict()
map_intfun_counter = 0

# ===== STEP 1: Reads in the binary of the loadable program ===== #
s = open(args[1]).read()
code = ["0x%02x" % (struct.unpack("B", i)) for i in s]

# ===== STEP 2: Allocation Table ===== #
var = {}   # var[variable_name] = (variable_size, allocated_addr)
alloc = {}   # alloc[variable_name] = ((offset, addr), (offset, addr), ...)
compact_alloc = []   # Final allocation table: [("real" addr, next patching addr), ...]
compact_alloc_binary = []
dataSection = {}
dataSection_values = []
dataSection_values_binary = []

# Gets variables' name and size
p = subprocess.Popen(["msp430-readelf", "-W", "-s", args[0]], stdout=subprocess.PIPE)
line = p.stdout.readline()
fm_addr = 0
while line:
    v = line.split()
    if len(v) == 8 and v[4] == "GLOBAL" and (v[6] == "COM" or v[6] == "3"):
        name = v[-1]
        if name != "TOS_NODE_ID":
            size = int(v[2])
            var[name] = [size, fm_addr]
            alloc[name] = []   # Filled later
            
            if v[6] == "3":
                dataSection[name] = [int(v[1], 16)]
                
            fm_addr += size
    line = p.stdout.readline()

# Gets the variables' location in the loadable program
p = subprocess.Popen(["msp430-readelf", "-W", "-r", args[0]], stdout=subprocess.PIPE)
line = p.stdout.readline()
while line:
    v = line.split()
    if len(v) == 7:
        name = v[4]
        if name in var and name != "TOS_NODE_ID":
            addr = int(v[0], 16)
            offset = int(v[-1], 16)
            alloc[name].append([offset, addr])
    line = p.stdout.readline()

# Patches the binary for address-chaining, and compacts the allocation table
for name in alloc.keys():
    alloc[name].sort(cmp)   # Sort by offset, then addr
    for i in range(len(alloc[name])):
        # Sees if address-chaining if necessary
        if (i + 1) < len(alloc[name]) and alloc[name][i][0] == alloc[name][i + 1][0]:
            code[alloc[name][i][1]] = "0x%02x" % ((alloc[name][i + 1][1]) & 0xFF)
            code[alloc[name][i][1] + 1] = "0x%02x" % ((alloc[name][i + 1][1] >> 8) & 0xFF)
        
        # Sees if the current entry should be included
        if i == 0 or (alloc[name][i - 1][0] != alloc[name][i][0]):
            real_addr = var[name][1] + alloc[name][i][0]   # "real" address = FM + offset
            compact_alloc.append("{%d, (void*)0x%04x}   /* %s + %d */" % (real_addr, alloc[name][i][1], name, alloc[name][i][0]))   # ["real" addr, next patching addr]
            compact_alloc_binary.append("0x%02x" % (real_addr & 0xFF))
            compact_alloc_binary.append("0x%02x" % ((real_addr >> 8) & 0xFF))
            compact_alloc_binary.append("0x%02x" % (alloc[name][i][1] & 0xFF))
            compact_alloc_binary.append("0x%02x" % ((alloc[name][i][1] >> 8) & 0xFF))
            
            if name in dataSection.keys():
                #print ".data:", real_addr, dataSection[name][0], var[name][0]
                dataSection_values_binary.append("0x%02x" % (real_addr & 0xFF))
                dataSection_values_binary.append("0x%02x" % ((real_addr >> 8) & 0xFF))
                dataSection_values_binary.append("0x%02x" % (dataSection[name][0] & 0xFF))
                dataSection_values_binary.append("0x%02x" % ((dataSection[name][0] >> 8) & 0xFF))
                dataSection_values_binary.append("0x%02x" % (var[name][0] & 0xFF))
                dataSection_values_binary.append("0x%02x" % ((var[name][0] >> 8) & 0xFF))

# ===== STEP 3: Full relocation table (compacted in step 5) ===== #
fun = []
global_fun = []
local_fun = []
# Gets both where functions are called and where it is located
p = subprocess.Popen(["msp430-readelf", "-W", "-s", args[0]], stdout=subprocess.PIPE)
line = p.stdout.readline()
while line:
    v = line.split()
    if len(v) == 8 and v[4] == "GLOBAL":
        if v[3] == "NOTYPE" or v[3] == "FUNC":
            fun.append(v[-1])
    line = p.stdout.readline()

# Gets global and local function calls and their locations in the loadable program
p = subprocess.Popen(["msp430-readelf", "-W", "-r", args[0]], stdout=subprocess.PIPE)
line = p.stdout.readline()
while line and line != "There are no relocations in this file.\n":
    v = line.split()
    if len(v) == 7:
        name = v[4]
        addr = int(v[0], 16)
        offset = int(v[-1], 16)
        if name in fun:
            if offset != 0:
                print "ERROR: Non zero offset for", name, "at", offset
            
            if map_extfun.has_key(name):
                global_fun.append([map_extfun[name], addr, name])
            else:
                if not map_intfun.has_key(name):
                  map_intfun[name] = [map_intfun_counter, 0]   # fun_id, addr
                  map_intfun_counter += 1
                local_fun.append([map_intfun[name][0], addr, name])
    line = p.stdout.readline()

# ===== STEP 4: Global and local symbol tables ===== #
global_sym = []
local_sym = []
global_sym_binary = []
compact_global_sym_binary = ["0x00", "0x00"]   # Just have address to one symbol (should be to main())
p = subprocess.Popen(["msp430-objdump", "-t", args[0]], stdout=subprocess.PIPE)
line = p.stdout.readline()
while line:
    v = line.split()
    if len(v) == 6 and \
       v[1] == "g" and v[2] == 'F' and v[3] == '.text':
        name = v[5]
        addr = int(v[0], 16)
        if map_hook.has_key(name):
            global_sym.append('{%d, (void*)0x%04x}   /* %s */' % (map_hook[name], addr, name))
            global_sym_binary.append("0x%02x" % (map_hook[name] & 0xFF))
            global_sym_binary.append("0x%02x" % ((map_hook[name] >> 8) & 0xFF))
            global_sym_binary.append("0x%02x" % (addr & 0xFF))
            global_sym_binary.append("0x%02x" % ((addr >> 8) & 0xFF))
            compact_global_sym_binary = ["0x%02x" % (addr & 0xFF)]
            compact_global_sym_binary.append("0x%02x" % ((addr >> 8) & 0xFF))
        else:
            if map_intfun.has_key(name):
                local_sym.append('{%s, (void*)0x%04x}   /* %s */' % (map_intfun[name][0], addr, name))
                map_intfun[name] = [map_intfun[name][0], addr]
    line = p.stdout.readline()

# ===== STEP 5: Patches the binary for address-chaining, and compacts the relocation table ===== #
global_fun_binary = []
local_fun_binary = []
# Patches the binary code
global_fun.sort(cmp)
for i in range(len(global_fun)):
    # Sees if address-chaining if necessary
    if (i + 1) < len(global_fun) and global_fun[i][0] == global_fun[i + 1][0]:
        code[global_fun[i][1]] = "0x%02x" % ((global_fun[i + 1][1]) & 0xFF)
        code[global_fun[i][1] + 1] = "0x%02x" % ((global_fun[i + 1][1] >> 8) & 0xFF)
local_fun.sort(cmp)
for i in range(len(local_fun)):
    # Sees if address-chaining if necessary
    if (i + 1) < len(local_fun) and local_fun[i][0] == local_fun[i + 1][0]:
        code[local_fun[i][1]] = "0x%02x" % ((local_fun[i + 1][1]) & 0xFF)
        code[local_fun[i][1] + 1] = "0x%02x" % ((local_fun[i + 1][1] >> 8) & 0xFF)

# Compacts the relocation table
i = 0
while True:
    if i >= len(global_fun):
        break
    
    if (i + 1) < len(global_fun) and (global_fun[i][0] == global_fun[i + 1][0]):
        del global_fun[i + 1]
    else:
        global_fun_binary.append("0x%02x" % (global_fun[i][0] & 0xFF))
        global_fun_binary.append("0x%02x" % ((global_fun[i][0] >> 8) & 0xFF))
        global_fun_binary.append("0x%02x" % (global_fun[i][1] & 0xFF))
        global_fun_binary.append("0x%02x" % ((global_fun[i][1] >> 8) & 0xFF))
        global_fun[i] = '{%d, (void*)0x%04x}   /* %s */' % (global_fun[i][0], global_fun[i][1], global_fun[i][2])
        i += 1
i = 0
while True:
    if i >= len(local_fun):
        break
    
    if (i + 1) < len(local_fun) and (local_fun[i][0] == local_fun[i + 1][0]):
        del local_fun[i + 1]
    else:
        local_fun_binary.append("0x%02x" % (map_intfun[local_fun[i][2]][1] & 0xFF))
        local_fun_binary.append("0x%02x" % ((map_intfun[local_fun[i][2]][1] >> 8) & 0xFF))
        local_fun_binary.append("0x%02x" % (local_fun[i][1] & 0xFF))
        local_fun_binary.append("0x%02x" % ((local_fun[i][1] >> 8) & 0xFF))
        local_fun[i] = '{%d, (void*)0x%04x}   /* %s */' % (map_intfun[local_fun[i][2]][1], local_fun[i][1], local_fun[i][2])
        i += 1

# ===== STEP 6: Prints out the image ===== #
#print "uint16_t g_sym_count = %d;" % (len(global_sym))
#print "uint16_t alloc_count = %d;" % (len(compact_alloc))
#print "uint16_t g_reloc_count = %d;" % (len(global_fun))
#print "uint16_t l_reloc_count = %d;" % (len(local_fun))
#print "uint16_t code_count = %d;" % (len(code))
#print
#
#print "uint8_t patch_table[] = {"
#print "\t%s,\n" % (",\n\t".join([", ".join(l) for l in slice(compact_alloc_binary, 16)]))   # Allocation table
#print "\t%s,\n" % (",\n\t".join([", ".join(l) for l in slice(global_fun_binary, 16)]))      # Global relocation table
#print "\t%s\n};" % (",\n\t".join([", ".join(l) for l in slice(local_fun_binary, 16)]))      # Local relocation table
#print
#print "struct value_addr_pair patch_table[] = {"
#print "\t%s,\n" % (",\n\t".join(compact_alloc))   # Allocation table
#print "\t%s,\n" % (",\n\t".join(global_fun))      # Global relocation table
#print "\t%s\n};" % (",\n\t".join(local_fun))      # Local relocation table
#print
#
#print "struct value_addr_pair g_syma[] = {\n\t%s\n};" % (",\n\t".join(global_sym))   # Global symbol table
#print "uint8_t g_sym[] = {\n\t%s\n};" % (",\n\t".join([", ".join(l) for l in slice(global_sym_binary, 16)]))
#print
#
#print "uint8_t code[] = {\n\t%s\n};" % (",\n\t".join([", ".join(l) for l in slice(code, 16)]))   # The binary code of the loadable program
#print

# Don't need it because local_fun has the following information already
## Local symbol table
#print "uint16_t l_sym_count = %d;" % (len(local_sym))
#print "struct addr_addr_pair l_sym[] = {\n\t%s\n};" % (",\n\t".join(local_sym))
#print

binary_image = compact_global_sym_binary
binary_image.extend(["0x%02x" % (i) for i in [#len(global_sym) & 0xFF, (len(global_sym) >> 8) & 0xFF,
                                              len(compact_alloc) & 0xFF, (len(compact_alloc) >> 8) & 0xFF,
                                              fm_addr & 0xFF, (fm_addr >> 8) & 0xFF,
                                              len(global_fun) & 0xFF, (len(global_fun) >> 8) & 0xFF,
                                              len(local_fun) & 0xFF, (len(local_fun) >> 8) & 0xFF,
                                              (len(dataSection_values_binary) / 6) & 0xFF, ((len(dataSection_values_binary) / 6) >> 8) & 0xFF,
                                              len(code) & 0xFF, (len(code) >> 8) & 0xFF]])

#binary_image.extend(global_sym_binary)
binary_image.extend(compact_alloc_binary)
binary_image.extend(global_fun_binary)
binary_image.extend(local_fun_binary)
binary_image.extend(dataSection_values_binary)
binary_image.extend(code)

#print len(code)

f = open(args[2], 'wb')
for i in binary_image:
    f.write(struct.pack("B", int(i, 16)))

if array_opt:
  print "uint8_t code[] = {\n\t%s\n};" % (",\n\t".join([", ".join(l) for l in slice(binary_image, 16)]))