Skip to content

Commit c0f2759

Browse files
theotherjimmyc1728p9
authored andcommitted
Add redirect support to toolchains
We create an API for generating the linker flags needed to redirect functions in an application
1 parent cd55625 commit c0f2759

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

tools/toolchains/__init__.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,51 @@ def binary(self, resources, elf, bin):
13451345
"""
13461346
raise NotImplemented
13471347

1348+
@staticmethod
1349+
@abstractmethod
1350+
def name_mangle(name):
1351+
"""Mangle a name based on the conventional name mangling of this toolchain
1352+
1353+
Positional arguments:
1354+
name -- the name to mangle
1355+
1356+
Return:
1357+
the mangled name as a string
1358+
"""
1359+
raise NotImplemented
1360+
1361+
@staticmethod
1362+
@abstractmethod
1363+
def make_ld_define(name, value):
1364+
"""Create an argument to the linker that would define a symbol
1365+
1366+
Positional arguments:
1367+
name -- the symbol to define
1368+
value -- the value to give the symbol
1369+
1370+
Return:
1371+
The linker flag as a string
1372+
"""
1373+
raise NotImplemented
1374+
1375+
@staticmethod
1376+
@abstractmethod
1377+
def redirect_symbol(source, sync, build_dir):
1378+
"""Redirect a symbol at link time to point at somewhere else
1379+
1380+
Positional arguments:
1381+
source -- the symbol doing the pointing
1382+
sync -- the symbol being pointed to
1383+
build_dir -- the directory to put "response files" if needed by the toolchain
1384+
1385+
Side Effects:
1386+
Possibly create a file in the build directory
1387+
1388+
Return:
1389+
The linker flag to redirect the symbol, as a string
1390+
"""
1391+
raise NotImplemented
1392+
13481393
# Return the list of macros geenrated by the build system
13491394
def get_config_macros(self):
13501395
return Config.config_to_macros(self.config_data) if self.config_data else []

tools/toolchains/arm.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
limitations under the License.
1616
"""
1717
import re
18-
from os.path import join, dirname, splitext, basename
18+
from os.path import join, dirname, splitext, basename, exists
19+
from os import makedirs, write
20+
from tempfile import mkstemp
1921

2022
from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS
2123
from tools.hooks import hook_tool
@@ -222,6 +224,22 @@ def binary(self, resources, elf, bin):
222224
self.cc_verbose("FromELF: %s" % ' '.join(cmd))
223225
self.default_cmd(cmd)
224226

227+
@staticmethod
228+
def name_mangle(name):
229+
return "_Z%i%sv" % (len(name), name)
230+
231+
@staticmethod
232+
def make_ld_define(name, value):
233+
return "--predefine=\"-D%s=0x%x\"" % (name, value)
234+
235+
@staticmethod
236+
def redirect_symbol(source, sync, build_dir):
237+
if not exists(build_dir):
238+
makedirs(build_dir)
239+
handle, filename = mkstemp(prefix=".redirect-symbol.", dir=build_dir)
240+
write(handle, "RESOLVE %s AS %s\n" % (source, sync))
241+
return "--edit=%s" % filename
242+
225243

226244
class ARM_STD(ARM):
227245
pass

tools/toolchains/gcc.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,18 @@ def binary(self, resources, elf, bin):
258258
self.cc_verbose("FromELF: %s" % ' '.join(cmd))
259259
self.default_cmd(cmd)
260260

261+
@staticmethod
262+
def name_mangle(name):
263+
return "_Z%i%sv" % (len(name), name)
264+
265+
@staticmethod
266+
def make_ld_define(name, value):
267+
return "-D%s=0x%x" % (name, value)
268+
269+
@staticmethod
270+
def redirect_symbol(source, sync, build_dir):
271+
return "-Wl,--defsym=%s=%s" % (source, sync)
272+
261273

262274
class GCC_ARM(GCC):
263275
@staticmethod

tools/toolchains/iar.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,15 @@ def binary(self, resources, elf, bin):
231231
# Exec command
232232
self.cc_verbose("FromELF: %s" % ' '.join(cmd))
233233
self.default_cmd(cmd)
234+
235+
@staticmethod
236+
def name_mangle(name):
237+
return "_Z%i%sv" % (len(name), name)
238+
239+
@staticmethod
240+
def make_ld_define(name, value):
241+
return "--config_def %s=0x%x" % (name, value)
242+
243+
@staticmethod
244+
def redirect_symbol(source, sync, build_dir):
245+
return "--redirect %s=%s" % (source, sync)

0 commit comments

Comments
 (0)