Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
/*
 * tramp.c
 *
 * DSP-BIOS Bridge driver support functions for TI OMAP processors.
 *
 * Copyright (C) 2009 Texas Instruments, Inc.
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "header.h"

#if TMS32060
#include "tramp_table_c6000.c"
#endif

#define MAX_RELOS_PER_PASS	4

/*
 * Function:	priv_tramp_sect_tgt_alloc
 * Description: Allocate target memory for the trampoline section.  The
 *	  target mem size is easily obtained as the next available address.
 */
static int priv_tramp_sect_tgt_alloc(struct dload_state *dlthis)
{
	int ret_val = 0;
	struct ldr_section_info *sect_info;

	/*  Populate the trampoline loader section and allocate it on the
	 * target.  The section name is ALWAYS the first string in the final
	 * string table for trampolines.  The trampoline section is always
	 * 1 beyond the total number of allocated sections. */
	sect_info = &dlthis->ldr_sections[dlthis->allocated_secn_count];

	sect_info->name = dlthis->tramp.final_string_table;
	sect_info->size = dlthis->tramp.tramp_sect_next_addr;
	sect_info->context = 0;
	sect_info->type =
	    (4 << 8) | DLOAD_TEXT | DS_ALLOCATE_MASK | DS_DOWNLOAD_MASK;
	sect_info->page = 0;
	sect_info->run_addr = 0;
	sect_info->load_addr = 0;
	ret_val = dlthis->myalloc->dload_allocate(dlthis->myalloc,
						  sect_info,
						  ds_alignment
						  (sect_info->type));

	if (ret_val == 0)
		dload_error(dlthis, "Failed to allocate target memory for"
			    " trampoline");

	return ret_val;
}

/*
 * Function:	priv_h2a
 * Description: Helper function to convert a hex value to its ASCII
 *	  representation.  Used for trampoline symbol name generation.
 */
static u8 priv_h2a(u8 value)
{
	if (value > 0xF)
		return 0xFF;

	if (value <= 9)
		value += 0x30;
	else
		value += 0x37;

	return value;
}

/*
 * Function:	priv_tramp_sym_gen_name
 * Description: Generate a trampoline symbol name (ASCII) using the value
 *	  of the symbol.  This places the new name into the user buffer.
 *	  The name is fixed in length and of the form: __$dbTR__xxxxxxxx
 *	  (where "xxxxxxxx" is the hex value.
 */
static void priv_tramp_sym_gen_name(u32 value, char *dst)
{
	u32 i;
	char *prefix = TRAMP_SYM_PREFIX;
	char *dst_local = dst;
	u8 tmp;

	/*  Clear out the destination, including the ending NULL */
	for (i = 0; i < (TRAMP_SYM_PREFIX_LEN + TRAMP_SYM_HEX_ASCII_LEN); i++)
		*(dst_local + i) = 0;

	/*  Copy the prefix to start */
	for (i = 0; i < strlen(TRAMP_SYM_PREFIX); i++) {
		*dst_local = *(prefix + i);
		dst_local++;
	}

	/*  Now convert the value passed in to a string equiv of the hex */
	for (i = 0; i < sizeof(value); i++) {
#ifndef _BIG_ENDIAN
		tmp = *(((u8 *) &value) + (sizeof(value) - 1) - i);
		*dst_local = priv_h2a((tmp & 0xF0) >> 4);
		dst_local++;
		*dst_local = priv_h2a(tmp & 0x0F);
		dst_local++;
#else
		tmp = *(((u8 *) &value) + i);
		*dst_local = priv_h2a((tmp & 0xF0) >> 4);
		dst_local++;
		*dst_local = priv_h2a(tmp & 0x0F);
		dst_local++;
#endif
	}

	/*  NULL terminate */
	*dst_local = 0;
}

/*
 * Function:	priv_tramp_string_create
 * Description: Create a new string specific to the trampoline loading and add
 *	  it to the trampoline string list.  This list contains the
 *	  trampoline section name and trampoline point symbols.
 */
static struct tramp_string *priv_tramp_string_create(struct dload_state *dlthis,
						     u32 str_len, char *str)
{
	struct tramp_string *new_string = NULL;
	u32 i;

	/*  Create a new string object with the specified size. */
	new_string =
	    (struct tramp_string *)dlthis->mysym->dload_allocate(dlthis->mysym,
								 (sizeof
								  (struct
								   tramp_string)
								  + str_len +
								  1));
	if (new_string != NULL) {
		/*  Clear the string first.  This ensures the ending NULL is
		 * present and the optimizer won't touch it. */
		for (i = 0; i < (sizeof(struct tramp_string) + str_len + 1);
		     i++)
			*((u8 *) new_string + i) = 0;

		/*  Add this string to our virtual table by assigning it the
		 * next index and pushing it to the tail of the list. */
		new_string->index = dlthis->tramp.tramp_string_next_index;
		dlthis->tramp.tramp_string_next_index++;
		dlthis->tramp.tramp_string_size += str_len + 1;

		new_string->next = NULL;
		if (dlthis->tramp.string_head == NULL)
			dlthis->tramp.string_head = new_string;
		else
			dlthis->tramp.string_tail->next = new_string;

		dlthis->tramp.string_tail = new_string;

		/*  Copy the string over to the new object */
		for (i = 0; i < str_len; i++)
			new_string->str[i] = str[i];
	}

	return new_string;
}

/*
 * Function:	priv_tramp_string_find
 * Description: Walk the trampoline string list and find a match for the
 *	  provided string.  If not match is found, NULL is returned.
 */
static struct tramp_string *priv_tramp_string_find(struct dload_state *dlthis,
						   char *str)
{
	struct tramp_string *cur_str = NULL;
	struct tramp_string *ret_val = NULL;
	u32 i;
	u32 str_len = strlen(str);

	for (cur_str = dlthis->tramp.string_head;
	     (ret_val == NULL) && (cur_str != NULL); cur_str = cur_str->next) {
		/*  If the string lengths aren't equal, don't bother
		 * comparing */
		if (str_len != strlen(cur_str->str))
			continue;

		/*  Walk the strings until one of them ends */
		for (i = 0; i < str_len; i++) {
			/*  If they don't match in the current position then
			 * break out now, no sense in continuing to look at
			 * this string. */
			if (str[i] != cur_str->str[i])
				break;
		}

		if (i == str_len)
			ret_val = cur_str;
	}

	return ret_val;
}

/*
 * Function:	priv_string_tbl_finalize
 * Description: Flatten the trampoline string list into a table of NULL
 *	  terminated strings.  This is the same format of string table
 *	  as used by the COFF/DOFF file.
 */
static int priv_string_tbl_finalize(struct dload_state *dlthis)
{
	int ret_val = 0;
	struct tramp_string *cur_string;
	char *cur_loc;
	char *tmp;

	/*  Allocate enough space for all strings that have been created.  The
	 * table is simply all strings concatenated together will NULL
	 * endings. */
	dlthis->tramp.final_string_table =
	    (char *)dlthis->mysym->dload_allocate(dlthis->mysym,
						  dlthis->tramp.
						  tramp_string_size);
	if (dlthis->tramp.final_string_table != NULL) {
		/*  We got our buffer, walk the list and release the nodes as*
		 * we go */
		cur_loc = dlthis->tramp.final_string_table;
		cur_string = dlthis->tramp.string_head;
		while (cur_string != NULL) {
			/*  Move the head/tail pointers */
			dlthis->tramp.string_head = cur_string->next;
			if (dlthis->tramp.string_tail == cur_string)
				dlthis->tramp.string_tail = NULL;

			/*  Copy the string contents */
			for (tmp = cur_string->str;
			     *tmp != '\0'; tmp++, cur_loc++)
				*cur_loc = *tmp;

			/*  Pick up the NULL termination since it was missed by
			 * breaking using it to end the above loop. */
			*cur_loc = '\0';
			cur_loc++;

			/*  Free the string node, we don't need it any more. */
			dlthis->mysym->dload_deallocate(dlthis->mysym,
							cur_string);

			/*  Move our pointer to the next one */
			cur_string = dlthis->tramp.string_head;
		}

		/*  Update our return value to success */
		ret_val = 1;
	} else
		dload_error(dlthis, "Failed to allocate trampoline "
			    "string table");

	return ret_val;
}

/*
 * Function:	priv_tramp_sect_alloc
 * Description: Virtually allocate space from the trampoline section.  This
 *	  function returns the next offset within the trampoline section
 *	  that is available and moved the next available offset by the
 *	  requested size.  NO TARGET ALLOCATION IS DONE AT THIS TIME.
 */
static u32 priv_tramp_sect_alloc(struct dload_state *dlthis, u32 tramp_size)
{
	u32 ret_val;

	/*  If the next available address is 0, this is our first allocation.
	 * Create a section name string to go into the string table . */
	if (dlthis->tramp.tramp_sect_next_addr == 0) {
		dload_syms_error(dlthis->mysym, "*** WARNING ***  created "
				 "dynamic TRAMPOLINE section for module %s",
				 dlthis->str_head);
	}

	/*  Reserve space for the new trampoline */
	ret_val = dlthis->tramp.tramp_sect_next_addr;
	dlthis->tramp.tramp_sect_next_addr += tramp_size;
	return ret_val;
}

/*
 * Function:	priv_tramp_sym_create
 * Description: Allocate and create a new trampoline specific symbol and add
 *	  it to the trampoline symbol list.  These symbols will include
 *	  trampoline points as well as the external symbols they
 *	  reference.
 */
static struct tramp_sym *priv_tramp_sym_create(struct dload_state *dlthis,
					       u32 str_index,
					       struct local_symbol *tmp_sym)
{
	struct tramp_sym *new_sym = NULL;
	u32 i;

	/*  Allocate new space for the symbol in the symbol table. */
	new_sym =
	    (struct tramp_sym *)dlthis->mysym->dload_allocate(dlthis->mysym,
					      sizeof(struct tramp_sym));
	if (new_sym != NULL) {
		for (i = 0; i != sizeof(struct tramp_sym); i++)
			*((char *)new_sym + i) = 0;

		/*  Assign this symbol the next symbol index for easier
		 * reference later during relocation. */
		new_sym->index = dlthis->tramp.tramp_sym_next_index;
		dlthis->tramp.tramp_sym_next_index++;

		/*  Populate the symbol information.  At this point any
		 * trampoline symbols will be the offset location, not the
		 * final.  Copy over the symbol info to start, then be sure to
		 * get the string index from the trampoline string table. */
		new_sym->sym_info = *tmp_sym;
		new_sym->str_index = str_index;

		/*  Push the new symbol to the tail of the symbol table list */
		new_sym->next = NULL;
		if (dlthis->tramp.symbol_head == NULL)
			dlthis->tramp.symbol_head = new_sym;
		else
			dlthis->tramp.symbol_tail->next = new_sym;

		dlthis->tramp.symbol_tail = new_sym;
	}

	return new_sym;
}

/*
 * Function:	priv_tramp_sym_get
 * Description: Search for the symbol with the matching string index (from
 *	  the trampoline string table) and return the trampoline
 *	  symbol object, if found.  Otherwise return NULL.
 */
static struct tramp_sym *priv_tramp_sym_get(struct dload_state *dlthis,
					    u32 string_index)
{
	struct tramp_sym *sym_found = NULL;

	/*  Walk the symbol table list and search vs. the string index */
	for (sym_found = dlthis->tramp.symbol_head;
	     sym_found != NULL; sym_found = sym_found->next) {
		if (sym_found->str_index == string_index)
			break;
	}

	return sym_found;
}

/*
 * Function:	priv_tramp_sym_find
 * Description: Search for a trampoline symbol based on the string name of
 *	  the symbol.  Return the symbol object, if found, otherwise
 *	  return NULL.
 */
static struct tramp_sym *priv_tramp_sym_find(struct dload_state *dlthis,
					     char *string)
{
	struct tramp_sym *sym_found = NULL;
	struct tramp_string *str_found = NULL;

	/*  First, search for the string, then search for the sym based on the
	   string index. */
	str_found = priv_tramp_string_find(dlthis, string);
	if (str_found != NULL)
		sym_found = priv_tramp_sym_get(dlthis, str_found->index);

	return sym_found;
}

/*
 * Function:	priv_tramp_sym_finalize
 * Description: Allocate a flat symbol table for the trampoline section,
 *	  put each trampoline symbol into the table, adjust the
 *	  symbol value based on the section address on the target and
 *	  free the trampoline symbol list nodes.
 */
static int priv_tramp_sym_finalize(struct dload_state *dlthis)
{
	int ret_val = 0;
	struct tramp_sym *cur_sym;
	struct ldr_section_info *tramp_sect =
	    &dlthis->ldr_sections[dlthis->allocated_secn_count];
	struct local_symbol *new_sym;

	/*  Allocate a table to hold a flattened version of all symbols
	 * created. */
	dlthis->tramp.final_sym_table =
	    (struct local_symbol *)dlthis->mysym->dload_allocate(dlthis->mysym,
				 (sizeof(struct local_symbol) * dlthis->tramp.
						  tramp_sym_next_index));
	if (dlthis->tramp.final_sym_table != NULL) {
		/*  Walk the list of all symbols, copy it over to the flattened
		 * table. After it has been copied, the node can be freed as
		 * it is no longer needed. */
		new_sym = dlthis->tramp.final_sym_table;
		cur_sym = dlthis->tramp.symbol_head;
		while (cur_sym != NULL) {
			/*  Pop it off the list */
			dlthis->tramp.symbol_head = cur_sym->next;
			if (cur_sym == dlthis->tramp.symbol_tail)
				dlthis->tramp.symbol_tail = NULL;

			/*  Copy the symbol contents into the flat table */
			*new_sym = cur_sym->sym_info;

			/*  Now finaize the symbol.  If it is in the tramp
			 * section, we need to adjust for the section start.
			 * If it is external then we don't need to adjust at
			 * all.
			 * NOTE: THIS CODE ASSUMES THAT THE TRAMPOLINE IS
			 * REFERENCED LIKE A CALL TO AN EXTERNAL SO VALUE AND
			 * DELTA ARE THE SAME.  SEE THE FUNCTION dload_symbols
			 * WHERE DN_UNDEF IS HANDLED FOR MORE REFERENCE. */
			if (new_sym->secnn < 0) {
				new_sym->value += tramp_sect->load_addr;
				new_sym->delta = new_sym->value;
			}

			/*  Let go of the symbol node */
			dlthis->mysym->dload_deallocate(dlthis->mysym, cur_sym);

			/*  Move to the next node */
			cur_sym = dlthis->tramp.symbol_head;
			new_sym++;
		}

		ret_val = 1;
	} else
		dload_error(dlthis, "Failed to alloc trampoline sym table");

	return ret_val;
}

/*
 * Function:	priv_tgt_img_gen
 * Description: Allocate storage for and copy the target specific image data
 *	and fix up its relocations for the new external symbol.  If
 *	a trampoline image packet was successfully created it is added
 *	to the trampoline list.
 */
static int priv_tgt_img_gen(struct dload_state *dlthis, u32 base,
			    u32 gen_index, struct tramp_sym *new_ext_sym)
{
	struct tramp_img_pkt *new_img_pkt = NULL;
	u32 i;
	u32 pkt_size = tramp_img_pkt_size_get();
	u8 *gen_tbl_entry;
	u8 *pkt_data;
	struct reloc_record_t *cur_relo;
	int ret_val = 0;

	/*  Allocate a new image packet and set it up. */
	new_img_pkt =
	    (struct tramp_img_pkt *)dlthis->mysym->dload_allocate(dlthis->mysym,
								  pkt_size);
	if (new_img_pkt != NULL) {
		/*  Save the base, this is where it goes in the section */
		new_img_pkt->base = base;

		/*  Copy over the image data and relos from the target table */
		pkt_data = (u8 *) &new_img_pkt->hdr;
		gen_tbl_entry = (u8 *) &tramp_gen_info[gen_index];
		for (i = 0; i < pkt_size; i++) {
			*pkt_data = *gen_tbl_entry;
			pkt_data++;
			gen_tbl_entry++;
		}

		/*  Update the relocations to point to the external symbol */
		cur_relo =
		    (struct reloc_record_t *)((u8 *) &new_img_pkt->hdr +
					      new_img_pkt->hdr.relo_offset);
		for (i = 0; i < new_img_pkt->hdr.num_relos; i++)
			cur_relo[i].SYMNDX = new_ext_sym->index;

		/*  Add it to the trampoline list. */
		new_img_pkt->next = dlthis->tramp.tramp_pkts;
		dlthis->tramp.tramp_pkts = new_img_pkt;

		ret_val = 1;
	}

	return ret_val;
}

/*
 * Function:	priv_pkt_relo
 * Description: Take the provided image data and the collection of relocations
 *	  for it and perform the relocations.  Note that all relocations
 *	  at this stage are considered SECOND PASS since the original
 *	  image has already been processed in the first pass.  This means
 *	  TRAMPOLINES ARE TREATED AS 2ND PASS even though this is really
 *	  the first (and only) relocation that will be performed on them.
 */
static int priv_pkt_relo(struct dload_state *dlthis, tgt_au_t * data,
			 struct reloc_record_t *rp[], u32 relo_count)
{
	int ret_val = 1;
	u32 i;
	bool tmp;

	/*  Walk through all of the relos and process them.  This function is
	 * the equivalent of relocate_packet() from cload.c, but specialized
	 * for trampolines and 2nd phase relocations. */
	for (i = 0; i < relo_count; i++)
		dload_relocate(dlthis, data, rp[i], &tmp, true);

	return ret_val;
}

/*
 * Function:	priv_tramp_pkt_finalize
 * Description: Walk the list of all trampoline packets and finalize them.
 *	  Each trampoline image packet will be relocated now that the
 *	  trampoline section has been allocated on the target.  Once
 *	  all of the relocations are done the trampoline image data
 *	  is written into target memory and the trampoline packet
 *	  is freed: it is no longer needed after this point.
 */
static int priv_tramp_pkt_finalize(struct dload_state *dlthis)
{
	int ret_val = 1;
	struct tramp_img_pkt *cur_pkt = NULL;
	struct reloc_record_t *relos[MAX_RELOS_PER_PASS];
	u32 relos_done;
	u32 i;
	struct reloc_record_t *cur_relo;
	struct ldr_section_info *sect_info =
	    &dlthis->ldr_sections[dlthis->allocated_secn_count];

	/*  Walk the list of trampoline packets and relocate each packet.  This
	 * function is the trampoline equivalent of dload_data() from
	 * cload.c. */
	cur_pkt = dlthis->tramp.tramp_pkts;
	while ((ret_val != 0) && (cur_pkt != NULL)) {
		/*  Remove the pkt from the list */
		dlthis->tramp.tramp_pkts = cur_pkt->next;

		/*  Setup section and image offset information for the relo */
		dlthis->image_secn = sect_info;
		dlthis->image_offset = cur_pkt->base;
		dlthis->delta_runaddr = sect_info->run_addr;

		/*  Walk through all relos for the packet */
		relos_done = 0;
		cur_relo = (struct reloc_record_t *)((u8 *) &cur_pkt->hdr +
						     cur_pkt->hdr.relo_offset);
		while (relos_done < cur_pkt->hdr.num_relos) {
#ifdef ENABLE_TRAMP_DEBUG
			dload_syms_error(dlthis->mysym,
					 "===> Trampoline %x branches to %x",
					 sect_info->run_addr +
					 dlthis->image_offset,
					 dlthis->
					 tramp.final_sym_table[cur_relo->
							       SYMNDX].value);
#endif

			for (i = 0;
			     ((i < MAX_RELOS_PER_PASS) &&
			      ((i + relos_done) < cur_pkt->hdr.num_relos)); i++)
				relos[i] = cur_relo + i;

			/*  Do the actual relo */
			ret_val = priv_pkt_relo(dlthis,
						(tgt_au_t *) &cur_pkt->payload,
						relos, i);
			if (ret_val == 0) {
				dload_error(dlthis,
					    "Relocation of trampoline pkt at %x"
					    " failed", cur_pkt->base +
					    sect_info->run_addr);
				break;
			}

			relos_done += i;
			cur_relo += i;
		}

		/*  Make sure we didn't hit a problem */
		if (ret_val != 0) {
			/*  Relos are done for the packet, write it to the
			 * target */
			ret_val = dlthis->myio->writemem(dlthis->myio,
							 &cur_pkt->payload,
							 sect_info->load_addr +
							 cur_pkt->base,
							 sect_info,
							 BYTE_TO_HOST
							 (cur_pkt->hdr.
							  tramp_code_size));
			if (ret_val == 0) {
				dload_error(dlthis,
					    "Write to " FMT_UI32 " failed",
					    sect_info->load_addr +
					    cur_pkt->base);
			}

			/*  Done with the pkt, let it go */
			dlthis->mysym->dload_deallocate(dlthis->mysym, cur_pkt);

			/*  Get the next packet to process */
			cur_pkt = dlthis->tramp.tramp_pkts;
		}
	}

	return ret_val;
}

/*
 * Function:	priv_dup_pkt_finalize
 * Description: Walk the list of duplicate image packets and finalize them.
 *	  Each duplicate packet will be relocated again for the
 *	  relocations that previously failed and have been adjusted
 *	  to point at a trampoline.  Once all relocations for a packet
 *	  have been done, write the packet into target memory.  The
 *	  duplicate packet and its relocation chain are all freed
 *	  after use here as they are no longer needed after this.
 */
static int priv_dup_pkt_finalize(struct dload_state *dlthis)
{
	int ret_val = 1;
	struct tramp_img_dup_pkt *cur_pkt;
	struct tramp_img_dup_relo *cur_relo;
	struct reloc_record_t *relos[MAX_RELOS_PER_PASS];
	struct doff_scnhdr_t *sect_hdr = NULL;
	s32 i;

	/* Similar to the trampoline pkt finalize, this function walks each dup
	 * pkt that was generated and performs all relocations that were
	 * deferred to a 2nd pass.  This is the equivalent of dload_data() from
	 * cload.c, but does not need the additional reorder and checksum
	 * processing as it has already been done. */
	cur_pkt = dlthis->tramp.dup_pkts;
	while ((ret_val != 0) && (cur_pkt != NULL)) {
		/*  Remove the node from the list, we'll be freeing it
		 * shortly */
		dlthis->tramp.dup_pkts = cur_pkt->next;

		/*  Setup the section and image offset for relocation */
		dlthis->image_secn = &dlthis->ldr_sections[cur_pkt->secnn];
		dlthis->image_offset = cur_pkt->offset;

		/*  In order to get the delta run address, we need to reference
		 * the original section header.  It's a bit ugly, but needed
		 * for relo. */
		i = (s32) (dlthis->image_secn - dlthis->ldr_sections);
		sect_hdr = dlthis->sect_hdrs + i;
		dlthis->delta_runaddr = sect_hdr->ds_paddr;

		/*  Walk all relos in the chain and process each. */
		cur_relo = cur_pkt->relo_chain;
		while (cur_relo != NULL) {
			/*  Process them a chunk at a time to be efficient */
			for (i = 0; (i < MAX_RELOS_PER_PASS)
			     && (cur_relo != NULL);
			     i++, cur_relo = cur_relo->next) {
				relos[i] = &cur_relo->relo;
				cur_pkt->relo_chain = cur_relo->next;
			}

			/*  Do the actual relo */
			ret_val = priv_pkt_relo(dlthis,
						cur_pkt->img_pkt.img_data,
						relos, i);
			if (ret_val == 0) {
				dload_error(dlthis,
					    "Relocation of dup pkt at %x"
					    " failed", cur_pkt->offset +
					    dlthis->image_secn->run_addr);
				break;
			}

			/*  Release all of these relos, we're done with them */
			while (i > 0) {
				dlthis->mysym->dload_deallocate(dlthis->mysym,
						GET_CONTAINER
						(relos[i - 1],
						 struct tramp_img_dup_relo,
						 relo));
				i--;
			}

			/*  DO NOT ADVANCE cur_relo, IT IS ALREADY READY TO
			 * GO! */
		}

		/* Done with all relos.  Make sure we didn't have a problem and
		 * write it out to the target */
		if (ret_val != 0) {
			ret_val = dlthis->myio->writemem(dlthis->myio,
							 cur_pkt->img_pkt.
							 img_data,
							 dlthis->image_secn->
							 load_addr +
							 cur_pkt->offset,
							 dlthis->image_secn,
							 BYTE_TO_HOST
							 (cur_pkt->img_pkt.
							  packet_size));
			if (ret_val == 0) {
				dload_error(dlthis,
					    "Write to " FMT_UI32 " failed",
					    dlthis->image_secn->load_addr +
					    cur_pkt->offset);
			}

			dlthis->mysym->dload_deallocate(dlthis->mysym, cur_pkt);

			/*  Advance to the next packet */
			cur_pkt = dlthis->tramp.dup_pkts;
		}
	}

	return ret_val;
}

/*
 * Function:	priv_dup_find
 * Description: Walk the list of existing duplicate packets and find a
 *	  match based on the section number and image offset.  Return
 *	  the duplicate packet if found, otherwise NULL.
 */
static struct tramp_img_dup_pkt *priv_dup_find(struct dload_state *dlthis,
					       s16 secnn, u32 image_offset)
{
	struct tramp_img_dup_pkt *cur_pkt = NULL;

	for (cur_pkt = dlthis->tramp.dup_pkts;
	     cur_pkt != NULL; cur_pkt = cur_pkt->next) {
		if ((cur_pkt->secnn == secnn) &&
		    (cur_pkt->offset == image_offset)) {
			/*  Found a match, break out */
			break;
		}
	}

	return cur_pkt;
}

/*
 * Function:	priv_img_pkt_dup
 * Description: Duplicate the original image packet.  If this is the first
 *	  time this image packet has been seen (based on section number
 *	  and image offset), create a new duplicate packet and add it
 *	  to the dup packet list.  If not, just get the existing one and
 *	  update it with the current packet contents (since relocation
 *	  on the packet is still ongoing in first pass.)  Create a
 *	  duplicate of the provided relocation, but update it to point
 *	  to the new trampoline symbol.  Add the new relocation dup to
 *	  the dup packet's relo chain for 2nd pass relocation later.
 */
static int priv_img_pkt_dup(struct dload_state *dlthis,
			    s16 secnn, u32 image_offset,
			    struct image_packet_t *ipacket,
			    struct reloc_record_t *rp,
			    struct tramp_sym *new_tramp_sym)
{
	struct tramp_img_dup_pkt *dup_pkt = NULL;
	u32 new_dup_size;
	s32 i;
	int ret_val = 0;
	struct tramp_img_dup_relo *dup_relo = NULL;

	/*  Determinne if this image packet is already being tracked in the
	   dup list for other trampolines. */
	dup_pkt = priv_dup_find(dlthis, secnn, image_offset);

	if (dup_pkt == NULL) {
		/*  This image packet does not exist in our tracking, so create
		 * a new one and add it to the head of the list. */
		new_dup_size = sizeof(struct tramp_img_dup_pkt) +
		    ipacket->packet_size;

		dup_pkt = (struct tramp_img_dup_pkt *)
		    dlthis->mysym->dload_allocate(dlthis->mysym, new_dup_size);
		if (dup_pkt != NULL) {
			/*  Save off the section and offset information */
			dup_pkt->secnn = secnn;
			dup_pkt->offset = image_offset;
			dup_pkt->relo_chain = NULL;

			/*  Copy the original packet content */
			dup_pkt->img_pkt = *ipacket;
			dup_pkt->img_pkt.img_data = (u8 *) (dup_pkt + 1);
			for (i = 0; i < ipacket->packet_size; i++)
				*(dup_pkt->img_pkt.img_data + i) =
				    *(ipacket->img_data + i);

			/*  Add the packet to the dup list */
			dup_pkt->next = dlthis->tramp.dup_pkts;
			dlthis->tramp.dup_pkts = dup_pkt;
		} else
			dload_error(dlthis, "Failed to create dup packet!");
	} else {
		/*  The image packet contents could have changed since
		 * trampoline detection happens during relocation of the image
		 * packets.  So, we need to update the image packet contents
		 * before adding relo information. */
		for (i = 0; i < dup_pkt->img_pkt.packet_size; i++)
			*(dup_pkt->img_pkt.img_data + i) =
			    *(ipacket->img_data + i);
	}

	/*  Since the previous code may have allocated a new dup packet for us,
	   double check that we actually have one. */
	if (dup_pkt != NULL) {
		/*  Allocate a new node for the relo chain.  Each image packet
		 * can potentially have multiple relocations that cause a
		 * trampoline to be generated.  So, we keep them in a chain,
		 * order is not important. */
		dup_relo = dlthis->mysym->dload_allocate(dlthis->mysym,
					 sizeof(struct tramp_img_dup_relo));
		if (dup_relo != NULL) {
			/*  Copy the relo contents, adjust for the new
			 * trampoline and add it to the list. */
			dup_relo->relo = *rp;
			dup_relo->relo.SYMNDX = new_tramp_sym->index;

			dup_relo->next = dup_pkt->relo_chain;
			dup_pkt->relo_chain = dup_relo;

			/*  That's it, we're done.  Make sure we update our
			 * return value to be success since everything finished
			 * ok */
			ret_val = 1;
		} else
			dload_error(dlthis, "Unable to alloc dup relo");
	}

	return ret_val;
}

/*
 * Function:	dload_tramp_avail
 * Description: Check to see if the target supports a trampoline for this type
 *	  of relocation.  Return true if it does, otherwise false.
 */
bool dload_tramp_avail(struct dload_state *dlthis, struct reloc_record_t *rp)
{
	bool ret_val = false;
	u16 map_index;
	u16 gen_index;

	/*  Check type hash vs. target tramp table */
	map_index = HASH_FUNC(rp->TYPE);
	gen_index = tramp_map[map_index];
	if (gen_index != TRAMP_NO_GEN_AVAIL)
		ret_val = true;

	return ret_val;
}

/*
 * Function:	dload_tramp_generate
 * Description: Create a new trampoline for the provided image packet and
 *	  relocation causing problems.  This will create the trampoline
 *	  as well as duplicate/update the image packet and relocation
 *	  causing the problem, which will be relo'd again during
 *	  finalization.
 */
int dload_tramp_generate(struct dload_state *dlthis, s16 secnn,
			 u32 image_offset, struct image_packet_t *ipacket,
			 struct reloc_record_t *rp)
{
	u16 map_index;
	u16 gen_index;
	int ret_val = 1;
	char tramp_sym_str[TRAMP_SYM_PREFIX_LEN + TRAMP_SYM_HEX_ASCII_LEN];
	struct local_symbol *ref_sym;
	struct tramp_sym *new_tramp_sym;
	struct tramp_sym *new_ext_sym;
	struct tramp_string *new_tramp_str;
	u32 new_tramp_base;
	struct local_symbol tmp_sym;
	struct local_symbol ext_tmp_sym;

	/*  Hash the relo type to get our generator information */
	map_index = HASH_FUNC(rp->TYPE);
	gen_index = tramp_map[map_index];
	if (gen_index != TRAMP_NO_GEN_AVAIL) {
		/*  If this is the first trampoline, create the section name in
		 * our string table for debug help later. */
		if (dlthis->tramp.string_head == NULL) {
			priv_tramp_string_create(dlthis,
						 strlen(TRAMP_SECT_NAME),
						 TRAMP_SECT_NAME);
		}
#ifdef ENABLE_TRAMP_DEBUG
		dload_syms_error(dlthis->mysym,
				 "Trampoline at img loc %x, references %x",
				 dlthis->ldr_sections[secnn].run_addr +
				 image_offset + rp->vaddr,
				 dlthis->local_symtab[rp->SYMNDX].value);
#endif

		/*  Generate the trampoline string, check if already defined.
		 * If the relo symbol index is -1, it means we need the section
		 * info for relo later.  To do this we'll dummy up a symbol
		 * with the section delta and run addresses. */
		if (rp->SYMNDX == -1) {
			ext_tmp_sym.value =
			    dlthis->ldr_sections[secnn].run_addr;
			ext_tmp_sym.delta = dlthis->sect_hdrs[secnn].ds_paddr;
			ref_sym = &ext_tmp_sym;
		} else
			ref_sym = &(dlthis->local_symtab[rp->SYMNDX]);

		priv_tramp_sym_gen_name(ref_sym->value, tramp_sym_str);
		new_tramp_sym = priv_tramp_sym_find(dlthis, tramp_sym_str);
		if (new_tramp_sym == NULL) {
			/*  If tramp string not defined, create it and a new
			 * string, and symbol for it as well as the original
			 * symbol which caused the trampoline. */
			new_tramp_str = priv_tramp_string_create(dlthis,
								strlen
								(tramp_sym_str),
								 tramp_sym_str);
			if (new_tramp_str == NULL) {
				dload_error(dlthis, "Failed to create new "
					    "trampoline string\n");
				ret_val = 0;
			} else {
				/*  Allocate tramp section space for the new
				 * tramp from the target */
				new_tramp_base = priv_tramp_sect_alloc(dlthis,
						       tramp_size_get());

				/*  We have a string, create the new symbol and
				 * duplicate the external. */
				tmp_sym.value = new_tramp_base;
				tmp_sym.delta = 0;
				tmp_sym.secnn = -1;
				tmp_sym.sclass = 0;
				new_tramp_sym = priv_tramp_sym_create(dlthis,
							      new_tramp_str->
							      index,
							      &tmp_sym);

				new_ext_sym = priv_tramp_sym_create(dlthis, -1,
								    ref_sym);

				if ((new_tramp_sym != NULL) &&
				    (new_ext_sym != NULL)) {
					/*  Call the image generator to get the
					 * new image data and fix up its
					 * relocations for the external
					 * symbol. */
					ret_val = priv_tgt_img_gen(dlthis,
								 new_tramp_base,
								 gen_index,
								 new_ext_sym);

					/*  Add generated image data to tramp
					 * image list */
					if (ret_val != 1) {
						dload_error(dlthis, "Failed to "
							    "create img pkt for"
							    " trampoline\n");
					}
				} else {
					dload_error(dlthis, "Failed to create "
						    "new tramp syms "
						    "(%8.8X, %8.8X)\n",
						    new_tramp_sym, new_ext_sym);
					ret_val = 0;
				}
			}
		}

		/*  Duplicate the image data and relo record that caused the
		 * tramp, including update the relo data to point to the tramp
		 * symbol. */
		if (ret_val == 1) {
			ret_val = priv_img_pkt_dup(dlthis, secnn, image_offset,
						   ipacket, rp, new_tramp_sym);
			if (ret_val != 1) {
				dload_error(dlthis, "Failed to create dup of "
					    "original img pkt\n");
			}
		}
	}

	return ret_val;
}

/*
 * Function:	dload_tramp_pkt_update
 * Description: Update the duplicate copy of this image packet, which the
 *	  trampoline layer is already tracking.  This is call is critical
 *	  to make if trampolines were generated anywhere within the
 *	  packet and first pass relo continued on the remainder.  The
 *	  trampoline layer needs the updates image data so when 2nd
 *	  pass relo is done during finalize the image packet can be
 *	  written to the target since all relo is done.
 */
int dload_tramp_pkt_udpate(struct dload_state *dlthis, s16 secnn,
			   u32 image_offset, struct image_packet_t *ipacket)
{
	struct tramp_img_dup_pkt *dup_pkt = NULL;
	s32 i;
	int ret_val = 0;

	/*  Find the image packet in question, the caller needs us to update it
	   since a trampoline was previously generated. */
	dup_pkt = priv_dup_find(dlthis, secnn, image_offset);
	if (dup_pkt != NULL) {
		for (i = 0; i < dup_pkt->img_pkt.packet_size; i++)
			*(dup_pkt->img_pkt.img_data + i) =
			    *(ipacket->img_data + i);

		ret_val = 1;
	} else {
		dload_error(dlthis,
			    "Unable to find existing DUP pkt for %x, offset %x",
			    secnn, image_offset);

	}

	return ret_val;
}

/*
 * Function:	dload_tramp_finalize
 * Description: If any trampolines were created, finalize everything on the
 *	  target by allocating the trampoline section on the target,
 *	  finalizing the trampoline symbols, finalizing the trampoline
 *	  packets (write the new section to target memory) and finalize
 *	  the duplicate packets by doing 2nd pass relo over them.
 */
int dload_tramp_finalize(struct dload_state *dlthis)
{
	int ret_val = 1;

	if (dlthis->tramp.tramp_sect_next_addr != 0) {
		/*  Finalize strings into a flat table.  This is needed so it
		 * can be added to the debug string table later. */
		ret_val = priv_string_tbl_finalize(dlthis);

		/*  Do target allocation for section BEFORE finalizing
		 * symbols. */
		if (ret_val != 0)
			ret_val = priv_tramp_sect_tgt_alloc(dlthis);

		/*  Finalize symbols with their correct target information and
		 * flatten */
		if (ret_val != 0)
			ret_val = priv_tramp_sym_finalize(dlthis);

		/*  Finalize all trampoline packets.  This performs the
		 * relocation on the packets as well as writing them to target
		 * memory. */
		if (ret_val != 0)
			ret_val = priv_tramp_pkt_finalize(dlthis);

		/*  Perform a 2nd pass relocation on the dup list. */
		if (ret_val != 0)
			ret_val = priv_dup_pkt_finalize(dlthis);
	}

	return ret_val;
}

/*
 * Function:	dload_tramp_cleanup
 * Description: Release all temporary resources used in the trampoline layer.
 *	  Note that the target memory which may have been allocated and
 *	  written to store the trampolines is NOT RELEASED HERE since it
 *	  is potentially still in use.  It is automatically released
 *	  when the module is unloaded.
 */
void dload_tramp_cleanup(struct dload_state *dlthis)
{
	struct tramp_info *tramp = &dlthis->tramp;
	struct tramp_sym *cur_sym;
	struct tramp_string *cur_string;
	struct tramp_img_pkt *cur_tramp_pkt;
	struct tramp_img_dup_pkt *cur_dup_pkt;
	struct tramp_img_dup_relo *cur_dup_relo;

	/*  If there were no tramps generated, just return */
	if (tramp->tramp_sect_next_addr == 0)
		return;

	/*  Destroy all tramp information */
	for (cur_sym = tramp->symbol_head;
	     cur_sym != NULL; cur_sym = tramp->symbol_head) {
		tramp->symbol_head = cur_sym->next;
		if (tramp->symbol_tail == cur_sym)
			tramp->symbol_tail = NULL;

		dlthis->mysym->dload_deallocate(dlthis->mysym, cur_sym);
	}

	if (tramp->final_sym_table != NULL)
		dlthis->mysym->dload_deallocate(dlthis->mysym,
						tramp->final_sym_table);

	for (cur_string = tramp->string_head;
	     cur_string != NULL; cur_string = tramp->string_head) {
		tramp->string_head = cur_string->next;
		if (tramp->string_tail == cur_string)
			tramp->string_tail = NULL;

		dlthis->mysym->dload_deallocate(dlthis->mysym, cur_string);
	}

	if (tramp->final_string_table != NULL)
		dlthis->mysym->dload_deallocate(dlthis->mysym,
						tramp->final_string_table);

	for (cur_tramp_pkt = tramp->tramp_pkts;
	     cur_tramp_pkt != NULL; cur_tramp_pkt = tramp->tramp_pkts) {
		tramp->tramp_pkts = cur_tramp_pkt->next;
		dlthis->mysym->dload_deallocate(dlthis->mysym, cur_tramp_pkt);
	}

	for (cur_dup_pkt = tramp->dup_pkts;
	     cur_dup_pkt != NULL; cur_dup_pkt = tramp->dup_pkts) {
		tramp->dup_pkts = cur_dup_pkt->next;

		for (cur_dup_relo = cur_dup_pkt->relo_chain;
		     cur_dup_relo != NULL;
		     cur_dup_relo = cur_dup_pkt->relo_chain) {
			cur_dup_pkt->relo_chain = cur_dup_relo->next;
			dlthis->mysym->dload_deallocate(dlthis->mysym,
							cur_dup_relo);
		}

		dlthis->mysym->dload_deallocate(dlthis->mysym, cur_dup_pkt);
	}
}