Is there a way to exclude a function/symbol from a particular section in linker script? - linker-scripts

I have used EXCLUDE_FILE to explicitly omit placing certain sections of certain object files in the master object file that I want to generate. I was wondering if there is a way to omit out just a particular symbol rather than an entire section.
Example:
Say I have a library a.lib which consists of 1.o, 2.o and 3.o with .text and .data sections. .text section of 1.o contains func1, func2 and func3.
using EXCLUDE_FILE, I can only omit out the entire .text or .data section from 1.o. I want to be able to omit out only func1 and place func2 and func3. Is this possible?

If you use GCC, you can put your function into a seperate section. And move or omit the section with a linker script.
int f3(void) __attribute__((section(".excl")));
int f3(void) {
...
}
These tell GCC to compile f3 into .excl section. Then with you linker script you can place it somewhere else.
SECTIONS
{
.text :
{
*(.text)
}
....
.excl :
{
*(.excl)
}
}

Related

NetSuite SuiteScript - Constants And Inclusion

I have a NetSuite SuiteScript file (2.0) in which I want to include a small library of utilities I've built. I can do that fine, and access the functions in the included library. But I can't access the constants I've defined in that library - I have to re-declare them in the main file.
Here's the main file:
define(['N/record', 'N/search', './utils.js'],
function (record, search, utils) {
function pageInit(scriptContext) {
isUserAdmin = isCurrentUserAdmin(contextRecord);
if (isUserAdmin) {
alert('Administrator Role ID is ' + ADMINISTRATOR_ROLE);
// Do something for Admin users
}
return;
}
return {
pageInit: pageInit
};
});
You can see I include the file ./utils.js in it. Here's utils.js:
const ADMINISTRATOR_ROLE = 11;
function isCurrentUserAdmin(currentRecord) {
return ADMINISTRATOR_ROLE == nlapiGetRole();
}
That's the entire file - nothing else.
In the main file, the call to the function isCurrentUserAdmin works fine. It correctly tells me whether the current user is an admin. Note that I don't have to preface the call to isCurrentUserAdmin with utils. (utils.isCurrentUserAdmin doesn't work - it gives me the error JS_EXCEPTION TypeError utils is undefined). But when the code gets to the line that uses ADMINSTRATOR_ROLE, I get the error JS_EXCEPTION ReferenceError ADMINISTRATOR_ROLE is not defined. BTW, if I put the constant definition of ADMINISTRATOR_ROLE in the main file instead of utils.js, I get the same error when utils.js tries to use it. The only way I can get it to work is if I have the line defining the constant in both files.
Why does the inclusion work for the function, but not the constant? Am I including the library wrongly? I thought I'd have to use it as utils.isCurrentUserAdmin rather than just isCurrentUserAdmin, but to my surprise that's not the case, as I say above.
If you have utils.js like below, you can use utils.ADMINISTRATOR_ROLE and utils.isCurrentUserAdmin() in your main file.
/**
*#NApiVersion 2.0
*/
define ([],
function() {
const ADMINISTRATOR_ROLE = 11;
function isCurrentUserAdmin() {
// check here
}
return {
ADMINISTRATOR_ROLE: ADMINISTRATOR_ROLE,
isCurrentUserAdmin: isCurrentUserAdmin
};
});
Try
define(['N/record', 'N/search', 'SuiteScripts/utils']
You need to make sure any member you need to access in another module needs to be exported in the source module using the return statement

How to do multiple substitution using perl scripts

I have a bunch of scripts I wanted to replace some texts.
Context : We are using selenium for UI Automation. We used to store the references to the UiElements in a map. But we are now moving to use PageFactory (a class with all the UiElements declared as a string)
So when we used map, we had to call the UIelements as objectMap.getIdentifier("navigate.leftsidebar"). But now with PageFactory (its like instantiating a object, and the UIElements are declared as a string), I can access these UIelements objectPageFactory.navigate_leftsidebar (here navigate_leftsidebar is a String)
So I will need to go modify all my existing scripts. Is there a way I can write a script to get this replaced, than doing it manually ?
Below are the 3 scenarios that I will encounter :
Click(getElement(objectMap.getIdentifier("navigate.leftsidebar").replace("$Page", "Inbox")), "clicking on an element");
objectMap.getIdentifier("navigate.leftsidebar")
Click(objectMap.getIdentifier("navigate.leftsidebar"), "clicking on an element");
This is the expected output:
Click((objectPageFactory.navigate_leftsidebar("Inbox")), "clicking on an element");
objectPageFactory.navigate_leftsidebar
Click(objectPageFactory.navigate_leftsidebar, "clicking on an element");
Changes are :
"objectMap" to be renamed as "objectPageFactory"
There could be different types of map. if objectMap , it should be replaced as objectPageFactory; if loginMap, it should be changed as loginPageFactory
objectMap.getIdentifier("navigate.leftsidebar") >>>> objectFactory.navigate_leftsidebar (the String literal inside the bracket is separated by underscore instead of dots
getElement is not needed now
we used to have some dynamic UiElements (navigate.leftsidebar in this case), for which we used to call String.replace, now we are writing functions which will internally do a String.format
getElement(objectMap.getIdentifier("navigate.leftsidebar").replace("$Page", "Inbox")) >>>>> objectPageFactory.navigate_leftsidebar("Inbox")
I got a perl script from this link, which will do partial job sed command to replace dots.
I just need to add the different scenarios to this, is there a way ? the output should now have a pageFactory text too, based on which map
#! /usr/bin/perl
use strict ;
sub fix { $_ = shift ; s/"//g ; s/\./_/g ; return $_ }
while ( <> ) {
s/getElement\(objectMap\.getIdentifier\(("?[a-z.]+"?)\)/fix($1)/e ;
s/objectMap\.getIdentifier\(("?[a-z.]+"?)\)/fix($1)/e ;
print
}
This seems to provide the output you requested. I don't understand the language you're changing, so there might be corner cases it processes wrong. Make a backup before you change the files!
#!/usr/bin/perl
use warnings;
use strict;
sub fix {
my ($id) = #_;
return $id =~ s/[.]/_/gr
}
while (<>) {
s{getElement\((object|login)Map\.getIdentifier\("([^"]*)"\)\.replace\("\$Page", "([^"]*)"\)\)}
{"$1PageFactory." . fix($2) . qq(("$3"))}ge;
s{(object|login)Map\.getIdentifier\("([^"]*)"\)}
{"$1PageFactory." . fix($2)}ge;
print;
}

Get a source reference to all struct extensions in Specman

I am familiar with the collect command in Specman which returns all of the specified method's extensions. However, show source for a certain struct only returns the base struct definition and not all the extensions.
is there a command in Specman that is equivalent to collect but for structs/units?
there is no such command in Specman but since e is an incredibly flexible language you can add any command you may need using the powerful e macros.
For example, to implement what you want, you can create a macro that makes use of the reflection
Mechanism to get all layers of the desired struct and then print the relevant source lines:
define <struct_collect'command> "s_collect <any>" as {
var line_num:int;
var st:rf_struct = rf_manager.get_struct_by_name("<1>");
if (st==NULL) {
out(append("struct name does not exist : <1>"));
} else {
for each in st.as_a(rf_like_struct).get_layers() {
line_num=it.get_source_line_num();
out(append("In file ",it.get_module().get_name()," line ",line_num, " : ",files.get_text_lines(it.get_module().get_full_file_name(), line_num,line_num)));
};
};
};
you can improve on this macro, by writing the results to a file, or arrange it in a different way.
Cheers

unable to find the function definition in a perl codebase

I am rummaging through 7900+ lines of perl code. I needed to change a few things and things were going quite well even though i am just 36 hours into perl. I learned the basic constructs of the language and was able to get small things done. But then suddenly I have found a function call which does not have any definition anywhere. I grep'ed several times to check for all 'sub'. I could not find the functions definition. What am I missing ? Where is the definition of this function. I am quite sure this is a user defined function and not a library function(from its name i guessed this).
Please help me find this function's definition.
Here is a few lines from around the function usage.
(cfg_machine_isActive($ep)) {
staf_var_set($ep, VAR_PHASE, PHASE_PREP);
staf_var_set($ep, VAR_PREP, STATE_RUNNING);
} else {
cfg_machine_set_state($ep, STATE_FAILED);
}
}
$rc = rvt_deploy_library(); #this is the function that is the problem
dump_states() unless ($rc != 0);
Here is the answer:
(i could not post this an answer itself cos i dont have enough reputation)
I found that the fastest way to find the definition of an imported function in perl are the following commands:
>perl.exe -d <filename>.pl
This starts the debugger.
Then; do
b <name of the function/subroutine who's definition you are looking for>
in our case that would mean entering:
b rvt_deploy_library
next press 'c' to jump to the mentioned function/subroutine.
This brings the debugger to the required function. Now, you can see the line no. and location of the function/subroutine on the console.
main::rvt_deploy_library(D:/CAT/rvt/lib/rvt.pm:60):
There are a number of ways to declare a method in Perl. Here is an almost certainly incomplete list:
The standard way, eg. sub NAME { ... }
Using MooseX::Method::Signatures, method NAME (...) {...}
Assignment to a typeglob, eg. *NAME = sub {...};
In addition, if the package declares an AUTOLOAD function, then there may be no explicit definition of the method. See perlsub for more information.
You can inspect any perl value with the B module. In this case:
sub function_to_find {}
sub find_sub (\&) {
my $code = shift;
require B;
my $obj = B::svref_2object($code); # create a B::CV object from $code
print "$code:\n";
print " $$_[0]: $$_[1]\n" for
[file => $obj->FILE],
[line => $obj->GV->LINE],
[name => $obj->GV->NAME],
[package => $obj->STASH->NAME];
}
find_sub &function_to_find;
which prints something like:
CODE(0x80ff50):
file: so.pl
line: 7
name: function_to_find
package: main
B::Xref will show all functions declared in all the files used by your code.

iOS -- use macros to forward a bunch of messages?

ForwardInvocation does exist, but it is slow and has the annoying problem of compiler warnings. So that got me to thinking -- is there a way to use macroes to quickly implement a bunch of getter methods that get the property in question from another object?
For example, if I have a Car object, it might want to implement the following:
Car.h:
#class SparkPlug;
#class Engine;
. . .
-(int) nPistons;
-(float) horsepower;
-(SparkPlug*) sparkPlug;
Car.m:
. . .
-(int) nPistons {
return self.engine.nPistons;
}
-(float) horsepower {
return self.engine.horsepower;
}
-(SparkPlug*) sparkPlug {
return self.engine.sparkPlug;
}
Question -- would it be possible to set up some macroes so that by making one change somewhere, I could add another such method to both the header and implementation files?
e.g. MagicForwardingMacro (nPistons, int, engine);
Ideally, in such a way that the macroes would be reusable if I later wanted to later use a similar strategy to get the firstName, lastName, placeOfBirth, and dateOfBirth properties of a Person from his or her birthCertificate.
The easiest way is probably to add the methods dynamically:
Add the properties to a category so the compiler doesn't complain too much.
Clone a suitable IMP in +[NSObject resolveInstanceMethod:]. You'll need to poke the Objective-C runtime.
Elaborating on the second step:
For each type, add a method like
-(int)getEngineInt {
return (int()(id,SEL))(objc_msgSend)(engine, _cmd);
}
Note that for structs you need objc_msgSend_stret and for floats/doubles you might need objc_msgSend_fpret (I think you only need it on i386; not sure about AMD64). The easy hack to support both the simulator and device is something like (I forget the macro name GCC uses...)
#if __i386
#define objc_msgSend_fpret objc_msgSend
#endif
Now to implement +resolveInstanceMethod:, you need to know the class you're forwarding to ahead of time. Let's say it's Engine.
+(BOOL)instancesRespondToSelector:(SEL)name
{
return [Engine instancesRespondToSelector:name];
}
+(BOOL)resolveInstanceMethod:(SEL)name
{
// Do we want to super-call first or last? Who knows...
if ([super resolveInstanceMethod:name]) { return YES; }
// Find the return type, which determines the "template" IMP we call.
const char * returntype = [Engine instanceMethodSignatureForSelector:name].methodReturnType;
if (!returnType) { return NO; }
// Get the selector corresponding to the "template" by comparing return types...
SEL template = NULL;
if (0 == strcmp(returntype,#encode(int))
{
sel = #selector(getEngineInt);
}
else if (0 == strcmp(Returntype,#encode(float))
{
...
}
if (!sel) { return NO; }
Method m = class_getInstanceMethod(self,template);
return class_addMethod(self, name, method_getImplementation(m), method_getTypeEncoding(m));
}
Alternatively, there's a slightly undocumented method -forwardingTargetForSelector: which may be fast enough for your needs.
EDIT: Alternatively, you can loop over the properties/methods dynamically. There doesn't appear to be an obvious way to introspect categories, but you can define them in a protocol, do something like #interface Engine:NSObject<Engine> ... #interface Car(DynamicEngine)<Engine> and use objc_getProtocol("Engine") and then protocol_copyMethodDescriptionList()/protocol_copyPropertyList() to get the methods, and then add the getters. I'm not sure if properties are added to the "method description list". Also note that the "copy" functions do not copy methods/properties from superclasses, which (in this case) is what you want.
Sadly, I don't think Objective-C 2.0 properties will work for you because I don't think you can specify any kind of forwarding in the property declaration.
You can't have one macro that will insert text in two different places. However, you can use two macros like so:
//This could also take the third argument and discard it, if you like
#define FORWARDI(type, prop) - (type)prop;
#define FORWARDM(type, prop, owner) - (type)prop { return owner.prop; }
//In the header...
FORWARDI(float, nPistons)
//In the implementation...
FORWARDM(float, nPistons, self.engine)
If you don't mind the methods not showing up in the header file (for example, if you will only use these methods inside the class's implementation itself), you can just as well use the implementation file macro by itself.
This is agnostic to the type of the owner, but it should work with any expression.
I'm getting close to what I want. Some nagging details remain:
ForwardingInclude.h:
// no include guard; we want to be able to include this multiple times
#undef forward
#ifdef IMPLEMENTATION
#define forward(a, b, c) -(a) b { return [[self c] b]; }
#else
#define forward(a, b, c) -(a) b;
#endif
CarForwarding.h:
// again, no include guard
#include ForwardingInclude.h
forward(int, nPistons, engine)
forward(SparkPlug* sparkPlug, engine)
Car.h:
#interface Car: SomeSuperclass {
// some ivars
}
. . .
#include CarForwarding.h
Car.m:
. . .
#implementation Car
#define IMPLEMENTATION
#include CarForwarding.h
The nagging details:
1) I don't like that #define IMPLEMENTATION line. I want CarForwarding.h to somehow automatically detect whether or not it is currently being included inside an implementation.
2) It would be waaaaaay cool if I could have the stuff defined in the forwarding file somehow also appear in human-readable form in the header. Or better yet -- write the "forward" definitions directly into the Car.h file somehow, so I don't need the CarForwarding.h file at all.