| #!/usr/bin/perl |
| # |
| # Copyright Statement: |
| # -------------------- |
| # This software is protected by Copyright and the information contained |
| # herein is confidential. The software may not be copied and the information |
| # contained herein may not be used or disclosed except with the written |
| # permission of MediaTek Inc. (C) 2018 |
| # |
| # BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| # THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE") |
| # RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON |
| # AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| # NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| # SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| # SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH |
| # THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO |
| # NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S |
| # SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM. |
| # |
| # BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE |
| # LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| # AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| # OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO |
| # MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| # |
| # THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE |
| # WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF |
| # LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND |
| # RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER |
| # THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC). |
| # |
| #***************************************************************************** |
| #* |
| #* Filename: |
| #* --------- |
| #* AutoAdjustInputSection.pl |
| #* |
| #* Project: |
| #* -------- |
| #* |
| #* |
| #* Description: |
| #* ------------ |
| #* This scripts is used to parse the configuration of Auto-Placement symbols with order. |
| #* |
| #* Author: |
| #* ------- |
| #* Frank Hu (mtk12175) |
| #* |
| #****************************************************************************/ |
| |
| use strict; |
| use warnings; |
| |
| BEGIN { push @INC , './tools/' } # add additional library path |
| use FileInfoParser; |
| use CommonUtility; |
| use File::Basename; |
| use File::Spec; |
| |
| package Type; |
| use constant Log => 0; |
| use constant Warning => 1; |
| use constant Fatal => 2; |
| package main; |
| |
| my ($ToolChainBase, $themf, $INFOMAKELOG, $LDSFileName) = (@ARGV); |
| |
| &msg_handler(Type::Log, "Input file: $ToolChainBase, $themf, $INFOMAKELOG, $LDSFileName\n"); |
| my $ConfigfileDir = "./interface/layout/InputSection/"; |
| if(!-e $ConfigfileDir) { |
| &msg_handler(Type::Warning, "$ConfigfileDir does not exist, thus ignore this step ..."); |
| exit 0; |
| } |
| |
| my %MAKEFILE_OPTIONS; |
| my %CmplOption; |
| my %RelOption; |
| |
| FileInfo::PreProcess($themf, \%MAKEFILE_OPTIONS, $INFOMAKELOG, \%CmplOption, \%RelOption,1); |
| |
| my ($Readelf, $AR, $GCC); |
| my ($Project, $Flavor); |
| my ($Original_Project, $Original_Flavor); |
| my ($LibPath1Prefix, $LibPath2Prefix); |
| my ($tempdir, $tempInputFolder); |
| |
| &GetEnvInfo(); |
| |
| #record all the config info |
| my %ConfigInfoHash; |
| #record all the used lib and obj |
| my %libObjHash; |
| #extract the info of all symbols of a .obj |
| my %SymLogInfoHash; |
| #extract the info of all sections of a .obj |
| my %SecLogInfoHash; |
| #create a link between symbol name and the actual symbol name in .obj |
| my %SymConvertHash; |
| #check if duplicated symbols exist between different .ldf files |
| my %SymCheckCrossFiles; |
| #check if group name is unique in different lds files |
| my %SecCheck; |
| |
| opendir(DIR, $ConfigfileDir) || &msg_handler(Type::Fatal, "Fail to open $ConfigfileDir: $!", __LINE__); |
| my @FileList = grep { /\.ldf$/ && -f "$ConfigfileDir/$_" && !/^sample\.ldf$/ } readdir(DIR); |
| close DIR; |
| |
| map { &ParseConfigFile("$ConfigfileDir/$_") } (@FileList); |
| |
| &ParseLibObj(); |
| &CheckUserConfiguration(); |
| |
| &UpdateLDS($LDSFileName); |
| |
| &CleanTempFiles(); |
| |
| exit 0; |
| |
| sub GetEnvInfo |
| { |
| $AR = File::Spec->rel2abs($ToolChainBase."ar"); |
| $GCC = $ToolChainBase."gcc"; |
| $Readelf = File::Spec->rel2abs($ToolChainBase."readelf"); |
| |
| &msg_handler(Type::Fatal, "GCC tool does not exist", __LINE__) unless(-e $Readelf and -e $AR and -e $GCC); |
| |
| my $RelPath = File::Basename::dirname($INFOMAKELOG); |
| my $AbsPath = File::Spec->rel2abs($RelPath); |
| |
| $Original_Project = $MAKEFILE_OPTIONS{"ORIGINAL_PROJECT_NAME"}; |
| $Original_Flavor = $MAKEFILE_OPTIONS{"ORIGINAL_FLAVOR"}; |
| |
| if($AbsPath =~ m|/mcu/build/(\w+)/(\w+)/|) { |
| ($Project, $Flavor) = ($1, $2); |
| } |
| else { |
| &msg_handler(Type::Fatal, "Fail to get project and flavor", __LINE__); |
| } |
| |
| ($tempdir) = ($LDSFileName =~ /(.*)custom/); |
| $tempdir .= "tmp/"; |
| |
| $LibPath1Prefix = File::Spec->rel2abs("./mtk_rel/$Original_Project/$Original_Flavor/TARGET/lib/"); |
| $LibPath2Prefix = File::Spec->rel2abs("./build/$Project/$Flavor/bin/lib/"); |
| |
| $tempInputFolder = $tempdir . "CustomInputSection"; |
| system("rm -rf $tempInputFolder") if(-e $tempInputFolder); |
| system("mkdir -p $tempInputFolder"); |
| $? == 0 or &msg_handler(Type::Fatal, "Making $tempInputFolder failed $?", __LINE__); |
| |
| my $OptFile = $tempdir."PreProcessOpt.txt"; |
| my $cmpOpt = undef; |
| #Gather compiler options |
| foreach my $key ( keys %CmplOption ) { |
| next if($key eq lc $key); |
| if (defined $CmplOption{$key}) { |
| $cmpOpt .= " -D$key=$CmplOption{$key}"; |
| } else { |
| $cmpOpt .= " -D$key"; |
| } |
| } |
| |
| open (FILE, "> $OptFile") or &msg_handler(Type::Fatal, "$OptFile: open file error!", __LINE__); |
| print FILE $cmpOpt; |
| close FILE; |
| } |
| |
| sub ParseConfigFile |
| { |
| my ($ConfigFile) = (@_); |
| my $FileName = File::Basename::basename($ConfigFile); |
| |
| my $OptFile = $tempdir."PreProcessOpt.txt"; |
| #Preprocess CustomInputSection.txt |
| my $cmd = "$GCC -x c -E -undef \@$OptFile $ConfigFile"; |
| my $output = qx($cmd); |
| $? == 0 or &msg_handler(Type::Fatal, "$ConfigFile preprosseing failed $?\n$output", __LINE__, $FileName); |
| |
| #Tidy preprocessor output |
| my $tempOutput = undef; |
| map{ $tempOutput .= $_."\n" if (!($_ =~ /^#/)); } (split /\n/, $output); |
| |
| my ($SubSection, $SecName, $PreSecName, $bFlag, $sFlag) = (undef, undef, undef, 0, 0); |
| my %UniqHash; |
| my (@TotalSecArray, @TotalSymArray, @SecNameArray); |
| my $BreakRegExp = "[\\*]{20}\\s[<]\\s(GROUP_CFG_\\w+)\\s[>]\\s[\\*]{20}"; |
| |
| #split entire config file into several configuration groups. |
| foreach my $line (split /\n/, $tempOutput) { |
| if($bFlag == 0) { |
| $bFlag = 1 if(($line =~ m/^\[Symbol\]\s+\[Obj\]\s+\[Lib\]$/)); |
| next; |
| } |
| next if($line =~ m/^\s*$/); |
| |
| if($line =~ m/^\s*$BreakRegExp\s*$/) { |
| $SecName = $1; |
| &msg_handler(Type::Fatal, "$SecName has been deifned in $SecCheck{$SecName}", __LINE__, $FileName) if(exists $SecCheck{$SecName}); |
| $SecCheck{$SecName} = $FileName; |
| $sFlag = 1; |
| if(defined $SubSection) { |
| push @TotalSecArray, $SubSection; |
| push @SecNameArray, $PreSecName; |
| undef $SubSection; |
| undef $PreSecName; |
| } |
| next; |
| } |
| $SubSection .= $line."\n" if($sFlag == 1); |
| $PreSecName = $SecName; |
| } |
| push @TotalSecArray, $SubSection if(defined $SubSection); |
| push @SecNameArray, $PreSecName if(defined $SubSection); |
| |
| &msg_handler(Type::Fatal, "The num of section name is not equal to section num", __LINE__, $FileName) if($#TotalSecArray != $#SecNameArray); |
| for (my $i = 0; $i <= $#TotalSecArray; $i++) { |
| my @SymArray = (); |
| my $align; |
| my @tempArray = split /\n/, $TotalSecArray[$i]; |
| foreach my $line (@tempArray) { |
| if($line =~ m/^(\S+)\s+(\S+)\s+(\S+)\s*$/) { |
| if(exists $UniqHash{$1.$2.$3}) { |
| &msg_handler(Type::Warning, "[Duplicated Lines]: $line", __LINE__, $FileName); |
| next; |
| } |
| else { |
| $UniqHash{$1.$2.$3} = 0; |
| } |
| |
| if(not exists $SymCheckCrossFiles{$1.$2.$3}) { |
| $SymCheckCrossFiles{$1.$2.$3} = $FileName; |
| push @SymArray, [$1, $2, $3]; |
| $libObjHash{$3}{$2} = 0; |
| } |
| else { |
| my $preLdf = $SymCheckCrossFiles{$1.$2.$3}; |
| &msg_handler(Type::Warning, "$1 has been defined in $preLdf", __LINE__, $FileName); |
| next; |
| } |
| } elsif ($line =~ m/^ALIGN:(\w+)$/){ |
| $align = $1; |
| } else { |
| my $msg = "Illegal Line Format: $line"; |
| &msg_handler(Type::Fatal, "Illegal Line Format: $line", __LINE__, $FileName); |
| next; |
| } |
| } |
| $ConfigInfoHash{$FileName}{$SecNameArray[$i]}{"ALIGN"} = $align if($#SymArray >= 0); |
| $ConfigInfoHash{$FileName}{$SecNameArray[$i]}{"LIST"} = \@SymArray if($#SymArray >= 0); |
| } |
| &msg_handler(Type::Warning, "$FileName is an invalid file", __LINE__, $FileName) unless(exists $ConfigInfoHash{$FileName}); |
| } |
| |
| sub ParseLibObj |
| { |
| foreach my $lib (keys %libObjHash) { |
| my $LibPath1 = $LibPath1Prefix . "/" . $lib; |
| my $LibPath2 = $LibPath2Prefix . "/" . $lib; |
| my $LibPath = (-e $LibPath1) ? $LibPath1 : $LibPath2; |
| if(!-e $LibPath) { |
| delete $libObjHash{$lib}; |
| next; |
| } |
| |
| my @ObjArray = keys %{$libObjHash{$lib}}; |
| foreach my $obj (@ObjArray) { |
| my $ExtractObj = $lib . $obj; |
| my $SymLog = $ExtractObj . "." . "symbols.log"; |
| my $SecLog = $ExtractObj . "." . "sections.log"; |
| |
| next if(-f $SymLog && -f $SecLog); #have been parsed before. |
| my $tmp_dir = $tempdir . "CustomInputSection/"; |
| chdir ($tmp_dir) or &msg_handler(Type::Fatal, "Fail to dir to $tmp_dir", __LINE__); |
| |
| #extract the obj from lib |
| system("$AR -x $LibPath $obj 2> /dev/null"); |
| unless(-f $obj) { |
| delete $libObjHash{$lib}{$obj}; |
| next; |
| } |
| |
| #move the obj to certain folder |
| system("mv $obj $ExtractObj"); |
| $? == 0 or &msg_handler(Type::Fatal, "$ExtractObj moving failed", __LINE__); |
| |
| #generate SYM log |
| system("$Readelf --syms -W $ExtractObj > $SymLog"); |
| $? == 0 or &msg_handler(Type::Fatal, "Generating $SymLog failed", __LINE__); |
| |
| #generate SECTION log |
| system("$Readelf -S -W $ExtractObj > $SecLog"); |
| $? == 0 or &msg_handler(Type::Fatal, "Generating $SecLog failed", __LINE__); |
| |
| &ParseSymAndSecLog($SymLog, $SecLog, $lib, $obj); |
| chdir ($ENV{'PWD'}) or &msg_handler(Type::Fatal, "Fail to dir to $ENV{'PWD'}", __LINE__); |
| } |
| } |
| } |
| |
| sub ParseSymAndSecLog |
| { |
| my ($tempSymLog, $tempSecLog, $tempLib, $tempObj) = (@_); |
| my $key = $tempLib . $tempObj; |
| |
| open SYM_LOG, "< $tempSymLog" or &msg_handler(Type::Fatal, "Fail to open $tempSymLog", __LINE__); |
| while(my $line = <SYM_LOG>) { |
| # 17: 00000024 40 FUNC LOCAL DEFAULT [MIPS16] 9 ev_cmp_f |
| if($line =~ m/^\s*\d+\s*:\s*\S+\s+(\d+).*\s+(\d+)\s+(\w+)$/) { |
| my ($Symbol, $index, $size) = ($3, $2, $1); |
| $SymConvertHash{$Symbol} = $Symbol; |
| $SymLogInfoHash{$key}{$Symbol}{'index'} = $index; |
| $SymLogInfoHash{$key}{$Symbol}{'size'} = $size; |
| } |
| } |
| close SYM_LOG; |
| |
| open SEC_LOG, "< $tempSecLog" or &msg_handler(Type::Fatal, "Fail to open $tempSecLog", __LINE__); |
| while(my $line = <SEC_LOG>) { |
| # [22] .text.evshed_give PROGBITS 00000000 000bbc 00002c 00 AX 0 0 4 |
| if($line =~ m/^\s*\[\s*(\d+)\s*\]\s+(\S+)\s+/) { |
| my ($index, $Section) = ($1, $2); |
| if($Section =~ m/^(\.?\w+)\.\w+$/) { |
| $SecLogInfoHash{$key}{$index}{'type'} = $1; |
| $SecLogInfoHash{$key}{$index}{'valid'} = 'true'; |
| } |
| elsif($Section =~ m/^[^\.]*$/) { |
| $SecLogInfoHash{$key}{$index}{'type'} = $Section; |
| $SecLogInfoHash{$key}{$index}{'valid'} = 'false'; |
| } |
| } |
| } |
| close SEC_LOG; |
| } |
| |
| sub CheckUserConfiguration |
| { |
| foreach my $ldf (keys %ConfigInfoHash) { |
| my $FileName = $ldf; |
| #check the validity of user config |
| foreach my $group (keys %{$ConfigInfoHash{$ldf}}) { |
| my @FinalSymList = (); |
| my $CheckSectionType = undef; |
| |
| foreach my $item (@{$ConfigInfoHash{$ldf}{$group}{"LIST"}}) { |
| my $SectionType = undef; |
| my $SymName = $item->[0]; |
| my $key = $item->[2] . $item->[1]; |
| my $SymNameInObj; |
| |
| if(not exists $libObjHash{$item->[2]}) { #lib does not exist |
| my $tips = $item->[2] . " does not exist."; |
| &msg_handler(Type::Warning, "$SymName not found in Group" . "[$group]" . ": $tips", __LINE__, $FileName); |
| next; |
| } |
| elsif(not exists $libObjHash{$item->[2]}{$item->[1]}) { #obj can't be found in lib |
| my $tips = $item->[1] . " not exists in " . $item->[2]; |
| &msg_handler(Type::Warning, "$SymName not found in Group" . "[$group]" . ": $tips", __LINE__, $FileName); |
| next; |
| } |
| |
| if(not exists $SymConvertHash{$SymName}) { #the symbol not exists |
| my $tips = "If your symbol type is C++, please use the full name in MAP/SYM, such as [ipfcore_task_init] -> [_Z17ipfcore_task_initv]"; |
| &msg_handler(Type::Warning, "$SymName can't be found in Group" . "[$group]" . ", $tips", __LINE__, $FileName); |
| next; |
| } |
| else { |
| $SymNameInObj = $SymConvertHash{$SymName}; |
| } |
| |
| my $index = $SymLogInfoHash{$key}{$SymNameInObj}{'index'}; |
| |
| if(not exists $SecLogInfoHash{$key}{$index}) { #the input section does not exist |
| &msg_handler(Type::Warning, "can't find the input section of $SymNameInObj in Group" . "[$group]", __LINE__, $FileName); |
| next; |
| } |
| |
| if($SecLogInfoHash{$key}{$index}{'valid'} eq 'false') { #unsupported section type |
| $SectionType = $SecLogInfoHash{$key}{$index}{'type'}; |
| my $NewType = $SectionType . '.' . $item->[0]; |
| my $tips = "Unsupported Inputsection: " . $item->[0] . " -> $SectionType in Group" . "[$group]" . ", please use __attribute__ ((sections(\"$NewType\"))) to re-try."; |
| &msg_handler(Type::Warning, $tips, __LINE__, $FileName); |
| $CheckSectionType = undef; |
| next; |
| } |
| |
| $SectionType = $SecLogInfoHash{$key}{$index}{'type'}; |
| $CheckSectionType = $SectionType if(not defined $CheckSectionType); |
| if($CheckSectionType ne $SectionType) { #Inputsection type mismatch within one group |
| my $msg = "Inputsection type mismatch in Group" . "[$group]: " . $item->[0] . " -> " . $SectionType; |
| &msg_handler(Type::Warning, $msg, __LINE__, $FileName); |
| next; |
| } |
| |
| push @{$item}, $SymLogInfoHash{$key}{$SymNameInObj}{'size'}, $CheckSectionType; |
| push @FinalSymList, $item; |
| } |
| if($#FinalSymList >= 0) { |
| $ConfigInfoHash{$ldf}{$group}{"LIST"} = \@FinalSymList; |
| } |
| else { |
| delete $ConfigInfoHash{$ldf}{$group}{"LIST"}; |
| } |
| } |
| } |
| } |
| |
| sub UpdateLDS |
| { |
| my ($LDSFile) = (@_); |
| my $output = undef; |
| open LDS, "< $LDSFile" or &msg_handler(Type::Fatal, "Fail to open $LDSFile", __LINE__); |
| my $CacheLineSize = undef; |
| |
| while(my $line = <LDS>) { |
| #get the cache line size |
| $CacheLineSize = $1 if($line =~ m/^\s*CACHELINESIZE\s*=\s*(\d+);$/); |
| |
| if($line =~ m/^(\s*)\*\s*\(\s*(\.?)(\w+)(\*?)\s*\)$/) { |
| my $suffix = ($4 eq '*') ? '.*' : ''; |
| my ($space, $ResultLine) = ($1, undef); |
| my $RegularPattern = ($2 eq '.') ? ('\.' . $3) : $3; |
| foreach my $ldf (keys %ConfigInfoHash) { |
| foreach my $group (keys %{$ConfigInfoHash{$ldf}}) { |
| my $GroupType = $ConfigInfoHash{$ldf}{$group}{"LIST"}->[0]->[4]; |
| next unless($GroupType =~ m/^$RegularPattern$suffix$/); |
| |
| my ($SubLine, $GroupSize) = (undef, 0); |
| $SubLine = "$space$group\$\$Base = .;\n"; |
| foreach my $item (@{$ConfigInfoHash{$ldf}{$group}{"LIST"}}) { |
| my ($sym, $obj, $size, $type) = ($item->[0], $item->[1], $item->[3], $item->[4]); |
| $SubLine .= "${space}*$obj ($type.$sym.*)\n" . "${space}*$obj ($type.$sym)\n"; |
| $GroupSize += $size; |
| } |
| $SubLine .= "$space$group\$\$Limit = . ;\n$space$group\$\$Length = ABSOLUTE($group\$\$Limit - $group\$\$Base);\n\n"; |
| if (defined $ConfigInfoHash{$ldf}{$group}{"ALIGN"}){ |
| $SubLine = "\n" . $space . ". = ALIGN($ConfigInfoHash{$ldf}{$group}{\"ALIGN\"});" . "\n" . $SubLine; |
| } else{ |
| #if total size > 32, align 64B to gain L2$, else, align 32B |
| my $AlignSize = (($GroupSize > 32) ? 64 : ((defined $CacheLineSize) ? $CacheLineSize : 32)); |
| $SubLine = "\n" . $space . ". = ALIGN($AlignSize);" . "\n" . $SubLine; |
| } |
| $ResultLine .= $SubLine; |
| #avoid multiple match |
| delete $ConfigInfoHash{$ldf}{$group}; |
| } |
| } |
| $line = $ResultLine . $line if(defined $ResultLine); |
| } |
| $output .= $line; |
| } |
| close LDS; |
| |
| open LDS, "> $LDSFile" or &msg_handler(Type::Fatal, "Fail to open $LDSFile", __LINE__); |
| print LDS $output; |
| close LDS; |
| |
| &msg_handler(Type::Log, "\nLDS is updated successfully ^O^, please check $LDSFileName.", __LINE__); |
| } |
| |
| sub CleanTempFiles |
| { |
| system("rm -rf $tempInputFolder"); |
| $? == 0 or &msg_handler(Type::Fatal, "Deleting $tempInputFolder failed $?", __LINE__); |
| |
| my $OptFile = $tempdir."PreProcessOpt.txt"; |
| system("rm -f $OptFile"); |
| $? == 0 or &msg_handler(Type::Fatal, "Deleting $OptFile failed $?", __LINE__); |
| } |
| |
| sub msg_handler |
| { |
| my ($type, $msg, $line_no, $file_name) = (@_); |
| my $prompt_prefix; |
| if($type == Type::Log) { |
| print $msg . "\n"; |
| } |
| elsif($type == Type::Warning) { |
| $prompt_prefix = ">> AAIS Warning : "; |
| my $location = (defined $file_name) ? "[$file_name] " : ""; |
| print $prompt_prefix . $location . $msg . "\n"; |
| } |
| elsif($type == Type::Fatal) { |
| $prompt_prefix = ">> *** AAIS Fatal : "; |
| my $location = (defined $file_name) ? "[$file_name] " : ""; |
| $location .= "[at line $line_no] "; |
| die $prompt_prefix . $location . $msg . "\n"; |
| } |
| } |